Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
andre4598
Contributor II
Contributor II

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?

Labels (5)
11 Replies
Anonymous
Not applicable

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.

andre4598
Contributor II
Contributor II
Author

Thanks the first option worked. 

 

My job now looks like this 0683p000009Lv2o.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? 

Anonymous
Not applicable

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

Anonymous
Not applicable

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

andre4598
Contributor II
Contributor II
Author

 

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

0683p000009Lukm.png

 

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

 

Anonymous
Not applicable

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?

andre4598
Contributor II
Contributor II
Author

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.

Anonymous
Not applicable

Glad you got it working

Anonymous
Not applicable

HI,

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