Get an Image from REST-Service

Forum to discuss the new web client version of Servoy.

Get an Image from REST-Service

Postby rph » Thu Feb 24, 2022 3:48 pm

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
rph
 
Posts: 75
Joined: Wed Aug 10, 2011 11:44 am
Location: Cham, Switzerland

Re: Get an Image from REST-Service

Postby rgansevles » Sat Feb 26, 2022 2:42 pm

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
Rob Gansevles
Servoy
User avatar
rgansevles
 
Posts: 1927
Joined: Wed Nov 15, 2006 6:17 pm
Location: Amersfoort, NL

Re: Get an Image from REST-Service

Postby rph » Mon Feb 28, 2022 10:31 am

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
rph
 
Posts: 75
Joined: Wed Aug 10, 2011 11:44 am
Location: Cham, Switzerland


Return to Servoy NGClient

Who is online

Users browsing this forum: No registered users and 4 guests