Welcome! Log In Create A New Profile

Advanced

REST Webservice handling images

Posted by jaistar 
REST Webservice handling images
June 20, 2022 12:56PM
Hi everyone...

I am using rest webservice to store/ retrive data. it is working fine for text data but for the image i am using the following code and getting internal server error
-------------------------------------------------------------------------------------------------------------------------------
PROCEDURE MakeAdd()

sRecordContentInJSON is ANSI string
sClientPhoto is ANSI string= Encode(fLoadBuffer(IMG_Picturefrm),encodeBASE64URL)
vClient is Variant

vClient.m_nEmployeeID = EDT_IDfrm
vClient.m_sName = EDT_Namefrm
vClient.m_sPhone = EDT_Phonefrm

vClient.m_Photo = sClientPhoto

sRecordContentInJSON = VariantToJSON(vClient)

oRequest is httpRequest
oResponse is httpResponse

oRequest.URL = gsIPAddress+"EmployeeAdd"
oRequest..ContentType = "application/json"
oRequest..Content = sRecordContentInJSON
oRequest..Method = httpPost
oResponse = HTTPSend(oRequest)

bufAnswer is Buffer = oResponse.Content
sAnswer is ANSI string = AnsiToUnicode(bufAnswer)

IF ErrorOccurred THEN
Error(ErrorInfo(errFullDetails))
ELSE
IF oResponse.DescriptionStatusCode = "Ok" THEN
ToastDisplay("Client No."+sAnswer+"is added with success", toastShort,vaMiddle,haCenter)
ELSE
// ToastDisplay("Client No."+sAnswer+"is added with success", toastShort,vaMiddle,haCenter)
ToastDisplay(oResponse.DescriptionStatusCode+ "Problem something wrong",toastShort, vaMiddle, haCenter)
END
END

-----------------------------------------------------------------------------------------------

am i doing wrong? anybody can help?



Edited 2 time(s). Last edit at 06/21/2022 10:11AM by jaistar.
Re: REST Webservice handling images
June 20, 2022 03:20PM
Why not using the REST service function?

LOCAL
	sREST is restRequest
	sRESTResponse is restResponse

sREST..URL = "[httpbin.org];
sREST..UserAgent=""
sREST..User="usernamehere"
sREST..Password="passwordhere"
sREST..Method=httpPost
sREST..ContentType="application/x-www-form-urlencoded"
sREST..Content=sPOSTListen

sRESTResponse = RESTSend(sREST)
Re: REST Webservice handling images
June 21, 2022 08:12AM
Hi

Typically for uploading files (like a photo) you need to make use of a httpform in your REST request service.

Cheers

Peter
Re: REST Webservice handling images
June 21, 2022 12:01PM
I'm using sturctures directly like and then the Serialize() function
stPhotoRequest.sPhotoBase64 = Encode(bufPhoto,encodeBASE64URL)
stPhotoRequest.someotherinfo = ...
stPhotoRequest.someotherinfo = ...
Serialize(stPhotoRequest,sRequest,psdJSON)
HTTPRequest(sServer, "", "", sRequest, "application/octet-stream")
BTW: that is in WM-Android

And on the other end there is a webdev AWP page
        bufData = PageParameter(paramBuffer)
	Deserialize(stPhotoRequest,bufData,psdJSON)
	bufPhoto = Decode(stPhotoRequest.sPhotoBase64,encodeBASE64URL)

it works like a charm.

Arie
Re: REST Webservice handling images
June 21, 2022 12:08PM
Hi Peter,

I'm currently rewriting my AWP-page (see post above) coming from the old days to a REST-webservice.
What is the benefit of using httpform instead of a base64-buffer directly?

Arie
Re: REST Webservice handling images
June 21, 2022 01:30PM
Hi Arie

That wasn't clear to me in the original post that it concerns an awp page context...

I'm using a lot of external REST web services and when working with files to send towards the service all of them need an httpForm request to retrieve the data.

If you're using purely internal calls yourself you surely have a better control on how you implement this. I guess the httpForm way of working slices up the data transfer into multiple requests or so (not sure) which makes more sense if you're transferring large files. Sending a 20mb image encoded into a 30mb base64 string in a single json may not be as reliable from a transfer perspective... Or there are historical reasons... Hence the httpForm method I'm seeing almost everywhere else when transferring files over REST...

For huge files, you'll need to have some sort of slicing too when doing it purely via base64 encoded json in order not to hit time outs. AWS Serverless for example is hard set to a maximum of 30 seconds for any API Request. Doing transfers over slow mobile networks for big files will hit that limit probably pretty regularly.

Just my 2 cents

Peter
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: