Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Swiip
Contributor
Contributor

tRestClient call of api to get a picture

I use the tRestClient component to execute a GET on an API to get a picture in the answer.

I tried with Postman and i got a binary file with the extension ".bin".

If i rename this file with the extension ".jpg" i can see the picture.

But in Talend with tRestClient i can't generate the correct picture.

I tried to do this :

tRestClient => tmap => tFileOutputRaw

In the tmap i choose only the string (and not statusCode and body). In the tFileOutputRaw i generated a file. But it's not a picture. The size of this file is 24 ko. And with my test on Postman the size of the file is 25 ko. If i rename the file generated with the extension ".jpg" it isn't a picture.

Thank you very much for your help.

Have a good day

Labels (3)
1 Solution

Accepted Solutions
gjeremy1617088143
Creator III
Creator III

Hi, I think you can use tfileFetch component instead of tRestClient component. It will write directly the response into a file.

View solution in original post

45 Replies
gjeremy1617088143
Creator III
Creator III

Hi, TRestClient send you a Json response, you have to parse it via a parser component like TExtractJsonField.

Send me Love and Kudos

Swiip
Contributor
Contributor
Author

But when i launch the same api with Postman and i click on "Save Response" i have directly the binary file and not a Json answer.

gjeremy1617088143
Creator III
Creator III

could you send a screenshot of the body response in postman ?

Swiip
Contributor
Contributor
Author

In attachment.

 

Thanks

gjeremy1617088143
Creator III
Creator III

It's seem to be a binary of a JFIF

Swiip
Contributor
Contributor
Author

Yes in fact it's a photo. When i click on "Save Response" i have a response.bin

If i rename the extension in response.jpg i can see the photo.

gjeremy1617088143
Creator III
Creator III

You can convert this binary to a byte array then write it on a file with the java.io.FileOutputStream class.

gjeremy1617088143
Creator III
Creator III

   public static String byteArrayToFile(byte[] input, String filepath) {

try {

      java.io.File fp = new java.io.File(filepath);

      java.io.FileOutputStream fos = new java.io.FileOutputStream(fp);

      fos.write(input);

      fos.close();

      return null;

    } catch (IOException e) {

 

      return e.toString();

 

       }

      }

Swiip
Contributor
Contributor
Author