REST.patch: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Created page with "{{MacroFunction |name=REST.patch |trusted=true |version=1.5 |description= Perform an HTTP patch request, to the specified URL, to make a partial update to the specified resour...")
 
m (Taustin moved page rEST.patch to REST.patch without leaving a redirect)
 
(5 intermediate revisions by 3 users not shown)
Line 2: Line 2:
|name=REST.patch
|name=REST.patch
|trusted=true
|trusted=true
|version=1.5
|version=1.5.0
|description=
|description=
Perform an HTTP patch request, to the specified URL, to make a partial update to the specified resource.
Perform an HTTP patch request, to the specified URL, to make a partial update to the specified resource.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
REST.patch(url, payload, mediaType, getFullResponse)
REST.patch(url, payload, mediaType, getFullResponse)
REST.patch(url, payload, mediaType, headers, getFullResponse)
REST.patch(url, payload, mediaType, headers, getFullResponse)
</source>
</syntaxhighlight>


'''Parameters'''
'''Parameters'''
{{param|url|}}
{{param|url|String containing 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). Use true(1) to get full response.}}
'''Returns'''
'''Returns'''


Line 23: Line 23:


|example=
|example=
TEXT HERE
Update user data with contents of payload.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: baseURL = "https://reqres.in"]
[h: baseURL = "https://reqres.in"]
[h: path = "/api/users/2"]
[h: path = "/api/users/2"]
[h: mediaType = "application/json; charset=utf-8"]
[h: mediaType = "application/json; charset=utf-8"]
[h: getFullResponse = 0]
[h: getFullResponse = 1]


[h: payload = '{ "name": "morpheus", "job": "zion resident" }']
[h: payload = '{ "name": "morpheus", "job": "zion resident" }']
Line 37: Line 37:
<pre>
<pre>
[r: json.indent(response, 2)]
[r: json.indent(response, 2)]
</pre></source>
</pre></syntaxhighlight>
Returns:
Returns:
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
TEXT HERE
{
</source>
  "status": 200,
  "headers":  {
    "access-control-allow-origin": ["*"],
    "cf-ray": ["4b927aa4ea915414-LAX"],
    "content-type": ["application/json; charset=utf-8"],
    "date": ["Sun, 17 Mar 2019 22:42:16 GMT"],
    "etag": ["W/\"50-xvMYBAeY4FNOMM/22NyIcimiIYc\""],
    "expect-ct": ["max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""],
    "server": ["cloudflare"],
    "set-cookie": ["__cfduid=d97966d3e9434720192d81096687475ff1552862536; expires=Mon, 16-Mar-20 22:42:16 GMT; path=/; domain=.reqres.in; HttpOnly"],
    "x-powered-by": ["Express"]
  },
  "body":  {
    "name": "morpheus",
    "job": "zion resident",
    "updatedAt": "2019-03-17T22:46:47.729Z"
  }
}</syntaxhighlight>


|also=
|also=
[[RESTful Functions Overview|RESTful Functions Overview]]
[[RESTful Functions Overview|RESTful Functions Overview]]


|changes=
* '''1.5''' - Added to main MapTool build.
}}
}}
[[Category:RESTful Function]]
[[Category:RESTful Function]]

Latest revision as of 16:24, 10 May 2023

REST.patch() Function

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

Introduced in version 1.5.0
Perform an HTTP patch request, to the specified URL, to make a partial update to the specified resource.

Usage

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

Parameters

  • url - String containing 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). Use 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

Update user data with contents of payload.
[h: baseURL = "https://reqres.in"]
[h: path = "/api/users/2"]
[h: mediaType = "application/json; charset=utf-8"]
[h: getFullResponse = 1]

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

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

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

Returns:

{
  "status": 200,
  "headers":   {
    "access-control-allow-origin": ["*"],
    "cf-ray": ["4b927aa4ea915414-LAX"],
    "content-type": ["application/json; charset=utf-8"],
    "date": ["Sun, 17 Mar 2019 22:42:16 GMT"],
    "etag": ["W/\"50-xvMYBAeY4FNOMM/22NyIcimiIYc\""],
    "expect-ct": ["max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""],
    "server": ["cloudflare"],
    "set-cookie": ["__cfduid=d97966d3e9434720192d81096687475ff1552862536; expires=Mon, 16-Mar-20 22:42:16 GMT; path=/; domain=.reqres.in; HttpOnly"],
    "x-powered-by": ["Express"]
  },
  "body":   {
    "name": "morpheus",
    "job": "zion resident",
    "updatedAt": "2019-03-17T22:46:47.729Z"
  }
}

See Also