To handle an HTTP response, use the following methods in the Profile mappers assistant drawer:
| Function name | Description | Return type |
|---|---|---|
asString() |
Gets the response body as a string. | String |
headers() |
Gets the HTTP headers of the response. | |
asDebugString() |
Gets a string representation of the content of the object. | String |
getAllValues() |
Gets all the existing values for the requested header. | String |
getFirstValue() |
Gets the first value for the requested header. | String |
names() |
Gets the names of the existing headers. | String |
size() |
Gets the number of existing headers. | Number |
statusCode() |
Gets the HTTP status code of the response. | Number |
Response methods fictional example:
try {
const response = http.get('https://example.com/api/accounts')
.header('Accept', 'application/json')
.send();
const body = response.asString();
console.log('The body is:', body);
const statusCode = response.statusCode();
console.log('The status code is:', statusCode);
const headers = response.headers();
const headerNames = headers.names();
console.log('The headers are:');
for (const name of headerNames) {
const values = headers.getAllValues(name);
for (const value of values) {
console.log(`${name}: ${value}`);
}
}
} catch (error) {
console.error('An error occurred:', error);
}