Highlighted
One Star

How to call REST web service and get response in talend

I have REST web Service URL- http://XYZ:8080/WebApp/search/service
If I send following json format data using Advanced Rest Client(Chrome-extension) then I get O/p as below-
Input- 
            {
    "query": {
        "text": "i lOVE inDIA",
        "service":
    }
}

Output-
            {
        -response: {
-pos:
-0:  {
text: "lOVE"
type: "JJ"
pos: "ADJECTIVE"
}
-1:  {
text: "inDIA"
type: "NN"
pos: "NOUN"
}
-2:  {
text: "lOVE inDIA"
type: ""
pos: "NOUN"
}
}
}

I tried with tRestClient but not get succeeded.
How to implement it in Talend5.6.1
14 REPLIES
One Star

Re: How to call REST web service and get response in talend

I too, never got tRestClient to work for a RESTful call. However, I can successfully perform a REST call with the tREST component. I set the URL up as a context value and ensure that my input is in the appropriate JSON structure. Make sure to specify the HTTP Header of "Content-Type" to the value of "application/json". 
If you manage to get the tRestClient component working, please let me know as I'd much prefer to use that instead.
Fifteen Stars

Re: How to call REST web service and get response in talend

With the tRESTClient component, you need to specify the Content Type and Accept Type correctly and also supply the JSON to the "string" input column and not the "body" input. That is the mistake that I have seen most when trying to get it to work with JSON
Rilhia Solutions
Fifteen Stars

Re: How to call REST web service and get response in talend

For output as JSON, set the Accept Type as "any". For some reason setting it to JSON converts it to XML. Not sure why, but I just remembered this.
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Hi rhall_2.0,
How to supply JSON as a String input?
Fifteen Stars

Re: How to call REST web service and get response in talend

The tRESTClient component has an input schema with two columns; body and string. You need to supply the JSON to the string column and not the body column.
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Hi rhall_2.0,
                 Thanks for reply, I am just no understanding how to give json input to String column.
Please find attached screenshot.
Fifteen Stars

Re: How to call REST web service and get response in talend

If you have your JSON already formatted, just create a tFixedFlowInput component and connect it to the tRESTClient component. It will ask  "Do you want to get the schema of the target component?". Click OK. Then populate the "string" column in the tFixedFlowInput component with your JSON. Either as a String surrounded with quotes or using a context variable.
This is a basic way of doing it. All you have to do is make sure that the component that feeds the tRESTClient component has a schema of "body" and "string", and the JSON code must be passed to the "string" column.
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Thanks for helping,
Yes I have formatted JSON but then also I am not able to recognize how to implement it.
I have connected tFixedFlowInput to tRESTClient, but where to give JSON data?
should I use tFileInputJson Component for that?
Please find screenshot & Suggest me.

Fifteen Stars

Re: How to call REST web service and get response in talend

There should be two columns in the tFixedFlowInput component if you connected it as I described. You only have the "string" column. Delete it and add it again and then click "OK" when the  "Do you want to get the schema of the target component?" message appears.
The next bit is a little harder. You actually put the JSON code where it needs to be, but it must be formatted as a Java string. If you wanted to send ....

Hello, my name is Richard 
....as a Java String in this column, you would send it as follows.....
"Hello, my name is Richard"

It must be enclosed in quotes. However, your JSON string has quotes in it. So these need to be escaped. To escape a quote in Java you put a \ before the quote. So, the following JSON string .....

          {
    "query": {
        "text": "i lOVE inDIA",
        "service":
    }
}
..... would be represented as....

          "{ \"query\": { \"text\": \"i lOVE inDIA\",\"service\": }}"
Ideally, you would let Talend build your JSON and then you would not need to worry about this. 
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Thank you so much rhall_2.0 It's working now with tRESTClient.
Please find scrrenshot for detail info.

Fifteen Stars

Re: How to call REST web service and get response in talend

Glad I could be of assistance :-)
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Hi rhall,
One more doubt...Now I got response in string(json/xml). but how to extract particular tag's data from that string on the fly?
eg-
Input-
{
    "query": {
        "text": "i lOVE inDIA",
        "service":
    }
}
Output-
{"response":{"ner":}]}}
now here I want only PERSON from response string, so how to extract that?
Fifteen Stars

Re: How to call REST web service and get response in talend

Use the tExtractJSONFields component. You will need to look at the documentation for this as it is not always very straight forward working with the tExtract components.
Rilhia Solutions
One Star

Re: How to call REST web service and get response in talend

Hi rhall,
I have scenario like below-
col1   col2
1        a
1        b
1        c
2        p
2        q
3        x
3        y
3        z
I want Output as-
col1   col2
1       array={a,b,c}
2       array={p,q}
3       array={x,y,z}
I want to store col2 in array object for that i follow above instruction given by Link- https://www.talendforge.org/forum/viewt … 81#p165081.
but I am getting O/P like below-
col1   col2
1      
1      
1      
2      
2      
3      
3      
3