# http.patch

Retrieves data from an HTTP source using the **PATCH** method.

```javascript
await http.patch(url, queryData, "json", requestOptions);
```

# Input

| Variable | Required | Description |
|----------|----------|-------------|
| `url`    | yes      | The URL to send the PATCH request to. |
| `queryData` | optional | *(object)* Optional PATCH parameters. |
| `contentType` | optional | *(string)* The content type of the request. See **Content Type** below for details. |
| `requestOptions` | optional | *(object)* Additional request options such as custom headers. See **Request HTTP Headers** below. |

# Output

This function returns a Promise with the following data:

| Variable | Type | Description |
|----------|------|-------------|
| `success` | boolean | True if the command completed successfully. |
| `error`  | string | Error message if the command failed. |
| `statusCode` | number | *(On success)* The HTTP status code. |
| `headers` | array | *(On success)* Array containing the HTTP headers. |
| `body`   | string | *(On success)* The body of the reply as a string. |

# Comments

This function performs an HTTP **PATCH** request to the specified URL.  
The request body can be sent as a `queryData` object:

```javascript
var response = await http.patch("http://ptsv3.com/smartbots-playground", {
  param1: 1,
  param2: 2
});
```

You can use <https://ptsv2.com> to test your PATCH requests.

# Content Type

By default, the request is sent as `application/x-www-form-urlencoded`  
(i.e., key=value pairs joined by `&`).

```javascript
http.patch(url, queryString);
// or equivalently

http.patch(url, queryString, "form");
```

To send JSON instead, specify `"json"` as the `contentType` argument — this sets  
the content type to `application/json` and automatically encodes the data:

```javascript
http.patch(url, queryData, "json");
```

# Request HTTP Headers

You can set HTTP headers using the `requestOptions` object.  
Currently, headers are the only supported option.

```javascript
var response = await http.patch(
  "http://ptsv3.com/smartbots-playground",
  { param1: 1, param2: 2 },
  "json",
  {
    "headers": {
       "x-my-header-1": "value-1",
       // ...
    }
  }
);
```

# Limitations

The playground responds with HTTP statusCode **206** when the response is too large. The returned response is trimmed to approximately 7168 bytes.

# Examples

A simple PATCH request using `await`:

```javascript
console.log("Doing http request...");

var response = await http.patch("http://ptsv3.com/smartbots-playground", { param1: 1, param2: 2 });
console.log("http result:", response.body);

// Gracefully stop the test script

exit();
```

A more complex example with error handling (using Promises):

```javascript
console.log("Doing http request...");

http.patch("http://ptsv3.com/smartbots-playground", { param1: 1, param2: 2 })
  .then(function(result) {
    if (!result.success) {
      // On error, display the error message and stop the processing
      console.log("HTTP error:", result.error);
      throw "";
    }

    console.log("http result:", result.body);
  })
  .catch(function(err) {
    // This block allows cancelling the processing chain with throw ""
    if (err != "") { throw err; }
  })
  .then(function() {
    // Gracefully stop the test script
    exit();
  });
```