Skip to content

Expect

Adds a status code that the response is allowed to have.

1
myRestRequest.Expect(status: string):RestRequest

Parameters

string status
    The status code name, such as Created or NotFound.

Returns

The current RestRequest instance.

Remarks

Without this method, each execution method uses its own default status codes. When Expect is used, only the expected status codes are accepted. Call it several times to accept several status codes.

The status code must be one of the following values from the Status column:

Examples

Accept the status code of a created resource

1
2
3
4
5
6
// Post accepts OK, Accepted and NoContent by default
var created = $Rest.Create('https://api.example.com/')
    .Request('customers', { name : $Xml.Evaluate('Customer/Name') })
    .Expect('Created')
    .Post()
    .ToJson();

Handle an expected error status

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var response = $Rest.Create('https://api.example.com/')
    .Request('customers/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('Customer/Id'))
    .Expect('OK')
    .Expect('NotFound')
    .Get();

if (response.StatusCode === 'NotFound') {
    $Xml.SetValue('Customer/Exists', false);
}
else {
    $Xml.SetValue('Customer/Name', response.ToJson()['name']);
}

See Also