Author Topic: REST Request Body  (Read 56 times)

PaulSamuelson

  • Newbie
  • *
  • Posts: 38
REST Request Body
« 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

td

  • Tech Support
  • *****
  • Posts: 2689
    • WinBatch
Re: REST Request Body
« Reply #1 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.  That suggests that your json belongs in the parameter to the "Send" method.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


PaulSamuelson

  • Newbie
  • *
  • Posts: 38
Re: REST Request Body
« Reply #2 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)

td

  • Tech Support
  • *****
  • Posts: 2689
    • WinBatch
Re: REST Request Body
« Reply #3 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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates