Call tRestClient with Post by passing Json body

Highlighted
Six Stars

Call tRestClient with Post by passing Json body

Hello,

 

I am actually starting to learn Talend and I have this scenario: I need to call Rest WS, get the results, transform data so that it respect q certqin metadata Schema, then call another Rest Webservice, to post the data. What I used as components are : TRestClient -> tExtractJsonField ->TMap -> tRestWebservice. In the latest WS, I need :

- Send request one by one so should I use flowToIterate

- From the flow coming from the TMap, how can I prepare a Json body to send it in the Post of tRestClient.

 

I am getting an error / javax.ws.rs.WebApplicationException: HTTP 400 Bad Request 

 

talend-flow.PNG

Thanks 


Accepted Solutions
Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

Hi 

 

So shall I go for this solution instead of using a txmlMap

 

Capturejson.PNG

View solution in original post


All Replies
Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

Hi
tRestClient requries a document as input request, you need to use a tXMLMap instead of tMap to build the document. 

Or you can use tRest to call API and use tWriteJsonField to write a Json string and pass it to tRest.

Regards
Shong

----------------------------------------------------------
Talend | Data Agility for Modern Business
Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

Hello Shong

 

Thanks for the reply. Ok let's assume that I will use the txmlMap. So the flow will be like this 

ws-with_txml.PNG

I am right? Now I still have javax.ws.rs.WebApplicationException: HTTP 400 Bad Request

NB: I am trying to send only one record. here my tXmlMap

ws-with_txml2.PNG

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

Have you tried your API using Postman? Does it works? Can you show an example of Json string that are required as input request?
----------------------------------------------------------
Talend | Data Agility for Modern Business
Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

Yes I have tried with postman. This is the body that I send postman.PNG

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

The tXMLMap does not produce JSON. You need to send your JSON as a String to the tRestClient's "string" column. You also need to set the tRestClient up to receive JSON.

Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

Hi,

 

After using the tXMLMap, the output is as below


xml_output.PNG

 

How can I send it as Json String?

 

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

Create JSON and do not use the tXMLMap.

Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

Hi 

 

So shall I go for this solution instead of using a txmlMap

 

Capturejson.PNG

View solution in original post

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

That would be a better approach if you want to produce JSON

Highlighted
Five Stars

Re: Call tRestClient with Post by passing Json body

Did you manage to sort out how to execute the trestclient?

 How do you pass all field from input row to a json document inside the body of the trestclient.

Could trestclient body allow to mapped expected field to pass from the current row?

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

This is demonstrated in the solution image. If you read the rest of the post, the information is there.

Highlighted
Five Stars

Re: Call tRestClient with Post by passing Json body

Thanks for the hint, I have read the full post including image, I am still lost on what to do inside tmap and jsonwrite

Highlighted
Five Stars

Re: Call tRestClient with Post by passing Json body

I have sort it out: just need to put the loop in the first field int twritejsonfield,  check the remove root node and ensure trestclient is fully configured in json, I now have to understand why null are transformed in []

Highlighted
Community Manager

Re: Call tRestClient with Post by passing Json body

Great news! I try to give enough information in my posts so that others can figure it out for themselves for similar issues. I find that you learn the product better if you have to struggle a little on your own Smiley Wink

Highlighted
Five Stars

Re: Call tRestClient with Post by passing Json body

to fixe the null transformed [ ] : in Insert a tJavaRow with the following code right after your tWriteJsonField:

output_row.string = input_row.string.replaceAll(",?\"[a-zA-Z_0-9]*\":\\[\\]", "");

thanks to Jbalberge

initial post https://www.talendforge.org/forum/viewtopic.php?id=27791

Highlighted
Six Stars

Re: Call tRestClient with Post by passing Json body

In order to resolve the issue of Null array generated by the tWriteJsonField, yu need to declare the attribute as object and not String Like below

 

Talend Null.png

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read