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
00
1 0 6 86

Replies

Hi, 

Could you share your code with us?

Hi Henrique,

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

<Route Url="/newOrder" Method="post" Call="newOrder"/>

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.

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.

Where should I get the output HTML-page?

Thanks,

Simon

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

Thanks to you all, I got it working now.

Best regards,

Simon