Five Stars

How to pass json data from files into HTTP Body of tREST Component

Hi 

I tried searching online for help on this but could not find a solution.

 

I have a job where I need to POST json data by iterating through a number of json files to a web server.

I am using tREST Component which allows me to POST the data if I manually copy and paste it into the HTTP Body field. However I would have thought it be possible to pass through the json data dynamically as a variable to the HTTP Body field.

 

The layout of my job is as follows:

tFileList >>iterate>> tFileInputDelimited >>iterate>> tRest >>main>> tLogRow

 

Is anyone able to provide help with this?

Tags (2)
11 REPLIES
Eleven Stars

Re: How to pass json data from files into HTTP Body of tREST Component

You can do this. Where are you getting your JSON from? I will assume that you are getting it from your tFileInputDelimited. If so this will work.

 

Connect your tFileInputDelimited to a tFlowToIterate component. I am assuming that one of your columns contains your JSON. Lets assume that column is called "myJSON" and the row connecting the tFileInputDelimited to the tFlowToIterate is "row1". Now if you add the following code to your HTTP Body (exactly as shown) it will pass the JSON from your file to your tRest....

((String)globalMap.get("row1.myJSON"))


If you have to build your JSON from data in your file, then do that first and then pass it (completed in one column) to a tFlowToIterate and follow the same logic. 

FYI the tFlowToIterate works by assigning the row.column data to a HashMap using the row.column (name) as the key.

Rilhia Solutions
Five Stars

Re: How to pass json data from files into HTTP Body of tREST Component

Thanks the first option worked. 

 

My job now looks like this post_httpbody_trest2.PNG

 

 The tREST now attempts to POST the json data but I get this error message...

"[...]Bad Request","details":"error: instance type (string) does not match any allowed primitive type (allowed: [object])[...]"

 

Basically it wants an Object not a String type.

 

Is it possible to convert a String value into an Object? 

Eleven Stars

Re: How to pass json data from files into HTTP Body of tREST Component

First of all, you should not have two unconnected subjobs. These need to be linked (probably via an iterate link from the tFlowToIterate to the tRest).

 

With regard to casting from Object to String, that is easy in Java (so long as the variable IS a String). I assume you are using a globalMap variable. Use the following code....

 

((String)globalMap.get("myvar"))

The "(String)" tells Java to cast (change) the class. This will only work if the two classes are in the same class hierarchy

Rilhia Solutions
Eleven Stars

Re: How to pass json data from files into HTTP Body of tREST Component

Sorry, I didn't read your question properly. This *may* be because you have not connected the two subjobs and null is being passed to the Body. 

 

Having said that, it could also be connected to the content-type header. Try connecting the subjobs and run again. You will need to give as much info as you can because web service issues are not as easy to resolve remotely

Rilhia Solutions
Five Stars

Re: How to pass json data from files into HTTP Body of tREST Component

 

 I have linked the sub jobs and included tREST configuration details in the screenshot. 

post_httpbody_trest3.PNG

 

The content type is set to json. I still receive the same error as before.

 

Eleven Stars

Re: How to pass json data from files into HTTP Body of tREST Component

I just Googled your error message. It looks like the JSON is being passed OK. That is a JSON error message. I believe there is something wrong with the JSON being sent. Can you show us the JSON?

Rilhia Solutions
Five Stars

Re: How to pass json data from files into HTTP Body of tREST Component

I have got it working now. I found what the problem was. I needed to remove quotes that were outside the json message and also remove the escaped quotes from the message itself.

Eleven Stars

Re: How to pass json data from files into HTTP Body of tREST Component

Glad you got it working

Rilhia Solutions
Two Stars

Re: How to pass json data from files into HTTP Body of tREST Component

HI,

i am facing a similar issue can u please help me how to post json data to rest client
Two Stars

Re: How to pass json data from files into HTTP Body of tREST Component

Hi Andre,

 

I have a similar scenario, could you please help by posting the screenshot of each component.

 

Two Stars

Re: How to pass json data from files into HTTP Body of tREST Component

Hi

 

I am not able to stuff my JSON file into a single column. Could you help??