POST request with JSON data to tRESTClient component

Six Stars

POST request with JSON data to tRESTClient component

How can I create POST request with JSON data using tRESTClient component? I can only successfully send XML Document. Should I put JSON data inside XML element? It seems that is the only way to send JSON data. I am using talend 5.5.1.
Seventeen Stars

Re: POST request with JSON data to tRESTClient component

You need to set the request header Content-Type to application/json. And no do not mix XML with JSON, this is a terrible idea!
Six Stars

Re: POST request with JSON data to tRESTClient component

Which component should I use ? tRESTClient or tREST ? I would like to use tRESTClient but it only inputs "Document" type of data and JSON data are String type.
Seventeen Stars

Re: POST request with JSON data to tRESTClient component

The Document type is quite ok. This is the result of the tXMLMap or another document related components in Talend.
For an REST request you need an document.
Six Stars

Re: POST request with JSON data to tRESTClient component

So how can I use tRESTClient ? Right now the problem is that JSON data is in String and tRESTClient needs a Document data.
One Star

Re: POST request with JSON data to tRESTClient component

Hello,
In the component tRESTClient, there are two fields : 'body' and 'string'. You need to juste put your string json in the field 'string' of the tRESTClient.
Lin LIN
Consultant BI/Talend
Synaltic Group
Six Stars

Re: POST request with JSON data to tRESTClient component

when I put string into a tRestclient's string filed I always get null on the other side inside a service. But the body gets through. What am I doing wrong?
One Star

Re: POST request with JSON data to tRESTClient component

The other side you mean the output? In the output, there are also body and string. But it's not body and string of the input. If you choose the option 'Convert Response To DOM Document', the result of the ouput will be put in the body. If not, the result will be put in the string. 
Lin LIN
Consultant BI/Talend
Synaltic Group
Six Stars

Re: POST request with JSON data to tRESTClient component

On the other side I mean on the service side.
I have 2 jobs, one is consumer that has tRestClient component. The tRestClient component has 2 inputs body and string. I am inputing both body and string. 
The second jobs is a service. In this job tRestResponse component outputs the string and body that was inputed in previosuly mentioned tRestClient component.
My problem is that from tRestResponse component i get out body but not string. String is always null no metter the input.
What should I do so the string won't be null? Right now from tRestResponse component I would also like to recive a string not just body.
Six Stars

Re: POST request with JSON data to tRESTClient component

I am using talend 5.5.1 and there is no 'Convert Response To DOM Document' option?
Is there something I can do in 5.5.1 version to transfer JSON data to the service and not inside XML?
One Star

Re: POST request with JSON data to tRESTClient component

For the component tRestClient, you don't need to put both body and string in the same time. If you want to put a xml, you use body, if not, you put just in string. There is not the option 'Convert Response To DOM Document' in the version 5.5.1? I use the version 6. Could you put a screenshot of 'Advanced settings' for your component tRestClient?
Lin LIN
Consultant BI/Talend
Synaltic Group
Six Stars

Re: POST request with JSON data to tRESTClient component

For the component tRestClient, you don't need to put both body and string in the same time.

You need to, otherwise it won't compile.
You can see that there is now 'Convert Response To DOM Document' option.
Six Stars

Re: POST request with JSON data to tRESTClient component

You can see that there is now 'Convert Response To DOM Document' option.
Six Stars

Re: POST request with JSON data to tRESTClient component

One Star

Re: POST request with JSON data to tRESTClient component

Can you update your talend to 5.5.2? There is this option from the version 5.5.2.
Lin LIN
Consultant BI/Talend
Synaltic Group
Six Stars

Re: POST request with JSON data to tRESTClient component

no I cannot upgrade.
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

For the component tRestClient, you don't need to put both body and string in the same time.

You need to, otherwise it won't compile.
You can see that there is now 'Convert Response To DOM Document' option.
http://imgur.com/f7DfHAx

This is not strictly true. While you need to connect from one component to the tRestClient supplying a "string" and "body" column, they do not both need to actually be populated with anything. 
If you simply supply a correct formatted JSON String to the "string" column and leave the "body" column empty, the service you are calling will receive the JSON. It will be converted to XML and will be received via the body Document column (you will need to create this), but it will be received.
Six Stars

Re: POST request with JSON data to tRESTClient component

If you simply supply a correct formatted JSON String to the "string" column and leave the "body" column empty, the service you are calling will receive the JSON.

I cannot crete this kind of job.
Can you tell me how to set components to achieve this?
Right now I have consumer job with:
tfixedflow--tXMLmap--tRESTClient
And service with 
tRESTRequest--tXMLmap--RESTResponse
If I do like you said I get null inside 'string'.
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

I assume you mean that you receive null inside "string" in the service. The service will not receive a "string" value, it will receive a "body" document. The JSON will be translated to a XML document.
Six Stars

Re: POST request with JSON data to tRESTClient component

It doesnt work.
If on the client side I leave body empty and put JSON inside string and
on the service side if I try to get JSON from the body I get an runtime error.
But this works:
If I put JSON inside XML and inside body and If I set tRESTClient that accepts JSON, I get JSON on the service side from body.
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

You need to give us screenshots of your tRESTClient and your tRESTRequest configurations (schemas, Advanced tab, etc).
Six Stars

Re: POST request with JSON data to tRESTClient component

client side

service side

I didn't change any advance settings except log messages.
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

For the tRESTClient you should set both the Content Type and Accept Type to JSON. Only pass the JSON to the "string".
As a test for the service, connect the tRESTRequest to a tLogRow before the tXMLMap.
Six Stars

Re: POST request with JSON data to tRESTClient component

I added JSON to the string in the tRESTClient component so tXMLmap looks like this.

Now After running I get this error:
Exception in component tRESTClient_2
javax.ws.rs.NotFoundException


And on the service side I get:
Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
One Star

Re: POST request with JSON data to tRESTClient component

Use tMap instead of the txmlmap. If not, txmlmap will create the xml in the body even if you don't put anything in this field.
Lin LIN
Consultant BI/Talend
Synaltic Group
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

Did you use a tLogRow before the tMap? You need to see exactly what you are getting from the tRESTRequest.
Six Stars

Re: POST request with JSON data to tRESTClient component

Use tMap instead of the txmlmap. If not, txmlmap will create the xml in the body even if you don't put anything in this field.

I replaced tXMLMap on service side and client side with tMap. And now I get on clide side still:
Exception in component tRESTClient_2
javax.ws.rs.NotFoundException

And on the service side I get
java.io.IOException: Illegal character: <<>

And inside tlogrow before tMap
.-----------------------------------------------------------+------.
|                            tLogRow_6                             |
|=----------------------------------------------------------+-----=|
|body                                                       |string|
|=----------------------------------------------------------+-----=|
|<?xml version="1.0" encoding="UTF-8"?>
<ORDER>25000</ORDER>|null  |
'-----------------------------------------------------------+------'
Sixteen Stars

Re: POST request with JSON data to tRESTClient component

That is working then. Look at the body field. It has converted your JSON to XML
One Star

Re: POST request with JSON data to tRESTClient component

So there is still the problem? If yes, could you put some screenshots? For better managing your body, i advise you to choose the option 'Wrap JSON Request' in the component tRESTRequest if that exists.

Lin LIN
Consultant BI/Talend
Synaltic Group
Six Stars

Re: POST request with JSON data to tRESTClient component

That is working then. Look at the body field. It has converted your JSON to XML

How should I convert this to JSON ?
I checked 'Wrap JSON Request'.
So there is still the problem? If yes, could you put some screenshots?

I still have the problem