REST Request Body

Started by PaulSamuelson, January 08, 2019, 11:15:43 AM

Previous topic - Next topic

PaulSamuelson

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

td

Data is usually placed in the body of a POST request.  See https://www.w3schools.com/tags/ref_httpmethods.asp.  That suggests that your json belongs in the parameter to the "Send" method.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

PaulSamuelson

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)

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade