Get an Image from REST-Service

Hi

We call a webservice and the content-type of the response is “image/png” and the content-disposition is ‘attachment; filename= "snapshot.png’. In Postman the Image will be shown in the “Body”-Tab after the successfull call of the webservice.

I try now to do this with the Servoy “plugins.http” and to store the content in a file, so I can store it somewhere (file or db).

The webservice works so far, but I dont know how to handle the response. response.getMediaData() returns null, but response.getResponseBody() returns a String that starts with “PNG\r\n…”.

What do I have to do now with this String? How do I have to “convert” it, so I can store it in a file?

I’ve tried to write it down with “plugins.file.writeTXTFile”. This create a file that is exactly as large as if I story the response in Postman but there are a few special characters in the file that looks different. So obiously there are some “encoding” issues.

Any ideas will be very appreciated.

Best
Roland

Hi Roland,

You can only use one of response.getResponseBody() or response.getMediaData() since they both consume the contents of the response.

Try calling response.getMediaData() without calling response.getResponseBody() first, this should return the actual bytes of your image.

Hope this helps,

Rob

Hi Rob

Thanks a lot for your reply. This does the trick!

My problem was, that I had “response.getResponseBody()” in the debugger-perspective in the “Expression”-View. And as I debug through the code, I had a breakpoint and step through the code. But the expression was directly resolved after I got the response and before response.getMediaData() was executed and so response.getMediaData always returned “null”. :roll: :roll:

Thanks again and have a nice day.
Best
Roland