# Script.Util.HttpRequest

Performs an HTTP request against a specified URL. When you use this request handler’s `send()` method, it returns a [Script.Util.HttpResponse](/docs/marketing/marketing-cloud/guide/ssjs_platformContentSyndicationScriptUtilHttpResponse.md) object that contains the response data. Unlike the HTTP request functions in the SSJS Core and Platform libraries, you can use this handler to perform other types of HTTP requests, such as PUT, PATCH, or DELETE.

This request handler only works with HTTP on port 80 and HTTPS on port 443.

## Syntax

```
Script.Util.HttpRequest(1)
```

| Ordinal | Type   | Description                                    |
| ------- | ------ | ---------------------------------------------- |
| `1`     | String | Required. The URL to send the HTTP request to. |

## Methods

The `Script.Util.HttpRequest` request handler has the methods listed in this table.

| Method                      | Description                                                                                                    |
| --------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `clearHeaders()`            | Removes all custom headers for the request.                                                                    |
| `removeHeader(String)`      | Removes a specific custom header from the request.                                                             |
| `send()`                    | Sends the request and returns a `Script.Util.HttpRequest` object. This method times out after 30 seconds.      |
| `setHeader(String, String)` | Adds a custom headers to the request. If you use this method to specify a header, content caching is disabled. |

:::note
The value of the `host` header is always set to the domain of the URL that the request was sent to, and the value of the `content-length` header is always set to the number of bytes of data in the request body. You can’t use the `setHeader()` method to modify the values of either of these headers.
:::

## Additional Properties

The `Script.Util.HttpRequest` request handler has the configuration properties listed in this table.

| Property               | Type    | Description                                                                                                                                                                                         |
| ---------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `contentType`          | String  | The content type to use in requests, such as `application/json` or `application/x-www-form-urlencoded`.                                                                                             |
| `continueOnError`      | Boolean | Specifies how to handle errors. If the value is `true`, the request continues after receiving an error. If the value is `false`, the request produces an exception.                                 |
| `emptyContentHandling` | Boolean | Indicates what to do if the request doesn’t return any content. If the value is `false`, the request throws an exception. If the value is `true`, the call continues without throwing an exception. |
| `encoding`             | String  | The content encoding to use in requests, such as `UTF-8`.                                                                                                                                           |
| `method`               | String  | The HTTP verb to use in the request. Possible values: <ul><li>`GET`</li><li>`DELETE`</li><li>`HEAD`</li><li>`OPTIONS`</li><li>`PATCH`</li><li>`POST`</li><li>`PUT`</li></ul>                        |
| `postData`             | String  | The request body. This property is required for POST requests.                                                                                                                                      |
| `retries`              | Number  | The number of times to retry the request. After the final retry, the request throws an exception.                                                                                                   |

## Usage

To use this request handler, initialize it and provide the destination URL. Next, specify the properties of the request, such as the HTTP method to use and the content of the request body. This example issues a PUT request to a destination server.

```js
<script runat="server">
  /* Create an authentication string to pass as a request header */
  var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyFDTijiOiIxMjM0NTYexAmp1e";
  var auth = "Bearer " + token;

  /* Specify the request body as a string */
  var requestBody = '{"email": "ralph.vasquez@example.com","customerKey": "b714fdda-4a05-483c-972c-e04c9452b3aa"}';

  try {
    /* Initialize the request handler */
    var request = new Script.Util.HttpRequest("https://www.example.com/put");

    /* Set request headers */
    request.setHeader("Authentication", auth);
    request.setHeader("sample-header", "HeaderValue");

    /* Configure the request properties */
    request.method = "PUT";
    request.encoding = "UTF-8";
    request.postData = requestBody;
    request.contentType = "application/json";

    /* Send the request */
    var response = request.send();

    /* Format the response content as a string, and then parse the JSON in that
       string. This step is necessary to output the content of the
       Script.Util.HTTPResponse object. */
    var responseStr = String(response.content);
    var responseJSON = Platform.Function.ParseJSON(responseStr);

    /* Output the response body */
    Platform.Response.Write(Platform.Function.Stringify(responseJSON));
  } catch(e) {
    Platform.Response.Write(Platform.Function.Stringify(e));
  }
</script>
```

The example outputs the full body of the response received from the server.

## See Also

- [Script.Util.HttpGet](/docs/marketing/marketing-cloud/guide/ssjs_platformContentSyndicationScriptUtilHttpGet.md)
- [Script.Util.HttpResponse](/docs/marketing/marketing-cloud/guide/ssjs_platformContentSyndicationScriptUtilHttpResponse.md)
