Written by

Question Simon Bijl · Jan 4, 2021

REST-service how to process Form Data

Hello to all,

I'm trying to post some form data from a form I made in React to the backend of cache fetching a REST-service.
Receiving and processing a JSON-object is no problem but how can I handle Form Data?

The frontend is sending the form as form data and also includes one or more files.

What is the easiest way to process this data in my REST.Broker- class?

Best regards,

Simon

Product version: Caché 2017.1

Comments

Henrique Dias · Jan 4, 2021

Hi, 

Could you share your code with us?

0
Simon Bijl  Jan 4, 2021 to Henrique Dias

Hi Henrique,

I don't have any code for this api yet but I do have one for (for example) JSON:

ClassMethod newOrder() As %Status {

    S dataToProcess=%request.Content.Read(32000)
    s dataToJSON=$TR(dataToProcess,"'"," ")
    s dataToJSON=$TR(dataToProcess,"""","'")
    s ^simonupload(2)=dataToJSON
    S dataToJSON=##class(Util.JSON).Decode(dataToJSON)
    s ^simonupload(3)=dataToJSON
    s ^simonupload(4)=dataToJSON.GetAt("Opmerkingen")
    s ^simonupload(5)=dataToJSON.GetAt("Bijlagen").GetAt(1)    
    quit $$$OK
}

This works for receiving JSON.

But I don't know how to do it with Form Data.

0
Eduard Lebedyuk · Jan 4, 2021

If you don't know how to get any part of request/response/session the easiest way is to send your request and execute this code on your server:

set %response.ContentType = "html"
do ##class(%CSP.Utils).DisplayAllObjects()
quit $$$OK

It would output HTML page containing all required information.

I think %request.Get(name) is what you need.

0
Simon Bijl  Jan 4, 2021 to Eduard Lebedyuk

Where should I get the output HTML-page?

Thanks,

Simon

0
Eduard Lebedyuk  Jan 4, 2021 to Simon Bijl

It would be a response in whatever tool you sent your request.

0
Simon Bijl · Jan 5, 2021

Thanks to you all, I got it working now.

Best regards,

Simon

0