Http headers print with trest client

Nine Stars

Http headers print with trest client

Hi there

 

Is there any way I can print Http Headers with trest client? I am trying to do like this:

 

System.out.println(globalMap.get("tRESTClient_1_HEADERS")); 

 

But it is showing me null......

 

Screen Shot 2018-11-04 at 7.46.55 pm.png

 

Is there something I am doing wrong?

 

Moderator

Re: Http headers print with trest client

Hello,

After calling a Restful service using tRESTClient,  you can get the headers from the response by using tJavaRow component

Add a tJavaRow after tRESTClient, with the following code:

//access to global headers
java.util.Map resp_headers = (java.util.Map<String,java.util.List<String>>)globalMap.get("tRESTClient_1_HEADERS");

//iterate through the list of headers
System.out.println("\nResponse Headers list:");
for(String headerName: (java.util.Set<String>) resp_headers.keySet()) {
    String headerValue = ((java.util.List<String>)resp_headers.get(headerName)).get(0).toString();
    System.out.println("\t" + headerName + ": " +  headerValue);

 

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Nine Stars

Re: Http headers print with trest client

@xdshi: Sabrina, thanks for your answer. I figured this out.

In a similar way how do I read result from rest client using javarow or javaflex component?

Any help would be appreciated. Thanks.
Moderator

Re: Http headers print with trest client

Hello,

Are you referring to the JSON response of a tRESTClient POST request? It is the body column or the string column that contains the string json? If body column, it is a Document type, link tRestClient to a tXMLMap to extract the string Json, and then link the output to a tExtractJSONFields to extract the value from the string Json, the job looks like:
tRestClient--main--tXMLMap--main--tExtractJSONFields--tLogRow
If the string column contains the string json, link tRestClient to tExtractJSONFields and extract the value, the job looks like:
tRestClient--main--tExtractJSONFields--tLogRow

Feel free to let us know if it is what you are looking for.

Best regards

Sabrina

 

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Nine Stars

Re: Http headers print with trest client

@xdshi: Thsnks for your reply. I know this.

I have specific need where I need to get entire response from test client as string or object type and then start manipulating it in java.

If you could tell me how to get that using javarow or java flex, that would be fanatic.
Seven Stars

Re: Http headers print with trest client

This was helpful for me.
Four Stars

Re: Http headers print with trest client

Thanks  for the code.

 

//access to global headers
java.util.Map resp_headers = (java.util.Map<String,java.util.List<String>>)globalMap.get("tRESTClient_1_HEADERS");

//iterate through the list of headers
System.out.println("\nResponse Headers list:");
for(String headerName: (java.util.Set<String>) resp_headers.keySet()) {
    String headerValue = ((java.util.List<String>)resp_headers.get(headerName)).get(0).toString();
    System.out.println("\t" + headerName + ": " +  headerValue);

Above code help us in fetching the list of response headers in tRestClient, Is there is any way to fetch the request headers of tRestClient component.

 

Specially I am looking for custom headers that we sent in Advance Setting section of tRestClient. I am looking for code where I could fetch all request headers of tRestClient at once like above code give all response headers list and log them.

 

In attached picture i want to fetch all request headers intJavaRow_1 that I set in HTTP headers section of tRestClient

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

An API-First Approach to Modernizing Applications

Learn how to use an API-First Approach to Modernize your Applications

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog