# Response

A class that creates a definition for an HTTP response. If an exception occurs during processing, most of the properties in the response object are empty.

```JAVASCRIPT
// Gets the content of a PasteBin paste, assuming that we have received its key (`pasteBinKey`) in a prior request.
const http = require('@jetbrains/youtrack-scripting-api/http');
const connection = new http.Connection('http://pastebin.com/raw/');
connection.addHeader({name: 'Content-Type', value: 'text/plain'});
const response = connection.getSync(pasteBinKey, '');
if (response && response.code === 200) {
  let text = '';
  response.headers.forEach(function(header) {
    text += header.name + ': ' + header.value + '\n';
  });
  text += '\n' + response.response;
  issue.addComment(text);
}
```

## Properties

| Name | Type | Description |
| --- | --- | --- |
| body |  string  | Read-only. The response body. Contains the same value as the `response` property. If an exception occurs while processing the request, the value is `null`.   Available since 2024.2    |
| bodyAsStream |  Object  | Read-only. A byte stream representation of the response body. Contains the same value as the `responseAsStream` property. If an exception occurs while processing the request, the value is `null`.   Available since 2024.2    |
| code |  number  |  The HTTP status code that is assigned to the response. If an exception occurs during processing, the property is empty.   |
| exception |  Object  |  The exception that occurred during processing.   |
| headers |  Array.<{name: String, value: String}>  |  A collection of response headers. If an exception occurs during processing, the collection is empty.   |
| isSuccess |  boolean  |  An indication of the success or failure for the request. If the HTTP status code is between 200 (inclusive) and 400 (exclusive), this property is set to `true`.   |
| response |  string  |  The response body. If an exception occurs during processing, the response body is empty (`null`).   |
| responseAsStream |  Object  |  A byte stream representation of the response body. If an exception occurs during processing, the property is empty (`null`).   |

## Methods

### json

```JAVASCRIPT
json()
```

Parses the response body as JSON. This is the recommended way to read a JSON response. When the body is a string, it is equivalent to `JSON.parse(response.response)`. When the body has already been parsed into an object, the method returns it unchanged. Like `JSON.parse`, this method throws an exception when the body does not contain valid JSON. Before parsing the body, check the `isSuccess` or `code` values.

Available since 2024.2

Return Value
: | Type | Description |
: | --- | --- |
: | Object, Array | The parsed response body. |

Example
: ```JAVASCRIPT
: // Reads a JSON response from a third-party service.
: // The API key is stored in the app settings as a secret, so it is passed to the `addHeader` method as is.
: // Concatenating the API key with a string produces a string that contains a mask instead of the API key.
: const http = require('@jetbrains/youtrack-scripting-api/http');
: const connection = new http.Connection('https://api.example.com');
: connection.addHeader('X-Api-Key', ctx.settings.apiKey);
: const response = connection.getSync('customers/' + customerId);
: if (response.isSuccess) {
: const customer = response.json();
: ctx.issue.addComment('Customer plan: ' + customer.plan);
: } else {
: console.warn('Request failed: ' + response.code + ' ' + response.response);
: }
: ```

## See also

[http](v1-http.html) [Connection](v1-Connection.html)

