REST.post: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Created page with "{{MacroFunction |name=REST.post |trusted=true |version=1.5 |description= Perform an HTTP post request to the specified URL to create a resource. |usage= <source lang="mtmacro...")
 
No edit summary
Line 13: Line 13:


'''Parameters'''
'''Parameters'''
{{param|url|}}
{{param|url|String containging the URL to the resource or collection of resources.}}
{{param|payload|}}
{{param|payload|JSON object containing the key:value pairs.}}
{{param|mediaType|}}
{{param|mediaType|String containing a MIME type and charset. See example.}}
{{param|headers|}}
{{param|headers|JSON object containing header key:value pairs.}}
{{param|getFullResponse|Boolean (0:1) to get full response.}}
{{param|getFullResponse|Boolean (0:1). True(1) to get full response.}}
'''Returns'''
'''Returns'''
HTTP response as JSON (if full response) or server response, usually JSON but can be XML, HTML, or other formats.
HTTP response as JSON (if full response) or server response, usually JSON but can be XML, HTML, or other formats.
Line 40: Line 40:
Returns:
Returns:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
TEXT HERE
{
  "status": 201,
  "headers":  {
    "access-control-allow-origin": ["*"],
    "cf-ray": ["4b92503c1f49772a-LAX"],
    "content-length": ["84"],
    "content-type": ["application/json; charset=utf-8"],
    "date": ["Sun, 17 Mar 2019 22:13:19 GMT"],
    "etag": ["W/\"54-Iq8tAhIi7JekRXqEAyUkl9PsnwI\""],
    "expect-ct": ["max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""],
    "server": ["cloudflare"],
    "set-cookie": ["__cfduid=dd8f9e69613d9ab995b4365e36bcc2e181552860799; expires=Mon, 16-Mar-20 22:13:19 GMT; path=/; domain=.reqres.in; HttpOnly"],
    "x-powered-by": ["Express"]
  },
  "body":  {
    "name": "morpheus",
    "job": "leader",
    "id": "996",
    "createdAt": "2019-03-17T22:17:50.616Z"
  }
}
</source>
</source>



Revision as of 22:38, 17 March 2019

REST.post() Function

 Note: This function can only be used in a Trusted Macro

Introduced in version 1.5
Perform an HTTP post request to the specified URL to create a resource.

Usage

REST.post(url, payload, mediaType, getFullResponse)
REST.post(url, payload, mediaType, headers, getFullResponse)

Parameters

  • url - String containging the URL to the resource or collection of resources.
  • payload - JSON object containing the key:value pairs.
  • mediaType - String containing a MIME type and charset. See example.
  • headers - JSON object containing header key:value pairs.
  • getFullResponse - Boolean (0:1). True(1) to get full response.

Returns HTTP response as JSON (if full response) or server response, usually JSON but can be XML, HTML, or other formats.

Example

Create a user with a post request.
[h: baseurl = "https://reqres.in"]
[h: path = "/api/users"]
[h: mediaType = "application/json; charset=utf-8"]
[h: getFullResponse = 1]

[h: payload = '{ "name": "morpheus", "job": "leader" }']

[h: response = REST.post(baseurl + path, payload, mediaType, getFullResponse)]

<br>
<pre>
[r: json.indent(response, 2)]
</pre>

Returns:

{
  "status": 201,
  "headers":   {
    "access-control-allow-origin": ["*"],
    "cf-ray": ["4b92503c1f49772a-LAX"],
    "content-length": ["84"],
    "content-type": ["application/json; charset=utf-8"],
    "date": ["Sun, 17 Mar 2019 22:13:19 GMT"],
    "etag": ["W/\"54-Iq8tAhIi7JekRXqEAyUkl9PsnwI\""],
    "expect-ct": ["max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""],
    "server": ["cloudflare"],
    "set-cookie": ["__cfduid=dd8f9e69613d9ab995b4365e36bcc2e181552860799; expires=Mon, 16-Mar-20 22:13:19 GMT; path=/; domain=.reqres.in; HttpOnly"],
    "x-powered-by": ["Express"]
  },
  "body":   {
    "name": "morpheus",
    "job": "leader",
    "id": "996",
    "createdAt": "2019-03-17T22:17:50.616Z"
  }
}

See Also

Version Changes

  • 1.5 - Added to main MapTool build.