WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: PaulSamuelson on January 08, 2019, 11:15:43 AM

Title: REST Request Body
Post by: PaulSamuelson on January 08, 2019, 11:15:43 AM
With some help from the Tech Database and messages, I have been able to make some simple REST API calls. I can set Headers, including Basic Authorization. So far I have been able to decode all of the responses.

Now I need to include data in the request and I have no idea where to do it.

In order to add a record with 2 fields to FileMaker, I need to send the JSON string '{"fieldData":{"Number":123,"Text":"ABC"}}'
The FileMaker Data API documentation calls this Parameter Data. Other examples I have seen called it the body of the request.

Where does this data go? The response to the following request is "fieldData is missing".

cURL = baseURL:'/fmi/data/v1/databases/WorkFlowPlus/layouts/RestTest/records?'
req = URLEncode('{"fieldData":{"Number":123,"Text":"ABC"}}')
cURL = cURL:req
oHTTP.Open("POST", cURL, @FALSE)
auth = "Bearer ":token
oHTTP.SetRequestHeader("Content-Type", "application/json")
oHTTP.SetRequestHeader("Authorization", auth)

oHTTP.Send()
oHTTP.WaitForResponse()
JSON_Data = oHTTP.ResponseText
Pause("POST JSON DATA",JSON_Data)


Thanks,
Paul
Title: Re: REST Request Body
Post by: td on January 08, 2019, 01:39:49 PM
Data is usually placed in the body of a POST request.  See https://www.w3schools.com/tags/ref_httpmethods.asp (https://www.w3schools.com/tags/ref_httpmethods.asp).  That suggests that your json belongs in the parameter to the "Send" method.
Title: Re: REST Request Body
Post by: PaulSamuelson on January 08, 2019, 02:52:19 PM
Apparently the answer is a non-URLEncoded parameter in Send(), even though the documentation says encoded. Maybe the Send() method auto encodes?

cURL = baseURL:'/fmi/data/v1/databases/WorkFlowPlus/layouts/RestTest/records';
request = '{"fieldData":{"Number":123,"Text":"ABC"}}'

oHTTP.Open("POST", cURL, @FALSE)

auth = "Bearer ":token
oHTTP.SetRequestHeader("Content-Type", "application/json")
oHTTP.SetRequestHeader("Authorization", auth)

oHTTP.Send(request)

oHTTP.WaitForResponse()
JSON_Data = oHTTP.ResponseText
Pause("POST JSON DATA",JSON_Data)
Title: Re: REST Request Body
Post by: td on January 09, 2019, 08:07:23 PM
It is unlikely but not impossible that the Send method would URL encode request body information because POST requests can be binary, URL encoded, or just plain text.  It could be the case that the Send method does URL encoding when the content-type header indicates json but I have not seen any documentation that states that that is the case.