# OptionsRequest

## Overview

The `OptionsRequest` class is used to perform HTTP OPTIONS requests, allowing developers to interact with APIs to determine supported operations on a resource. It supports synchronous and asynchronous request execution with various authentication methods, providing flexibility for different use cases.

Headers can be added to requests using the `addHeader(headerName, value)` method, while the `executeRequest()` and `executeAsyncRequest()` methods handle synchronous and asynchronous execution, respectively. Asynchronous methods include callbacks for success and error handling, ensuring robust response management. Authentication options, including Windows authentication, are also supported.

Additional functionality includes retrieving allowed HTTP methods using `getAllowedMethods(res)` and configuring preemptive authentication with `usePreemptiveAuthentication(b)`. These features enable efficient API interaction and streamlined HTTP request handling.

For an overview of the HTTP client functionality and configuration options, refer to the [HTTP Client](https://docs.servoy.com/reference/servoyextensions/server-plugins/http) and [HTTP Client Config](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/httpclientconfig) documentation.

## Methods Summarized

| Type                                                                                        | Name                                                                                                                                                                                                                                                 | Summary                                                                                                                                                                                                                                                                                                   |
| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)              | [addHeader(headerName, value)](#addheader-headername-value)                                                                                                                                                                                          | Add a header to the request.                                                                                                                                                                                                                                                                              |
| [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise)              | [executeAsyncRequest()](#executeasyncrequest)                                                                                                                                                                                                        | Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.                                                                                                                                                                 |
| [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise)              | [executeAsyncRequest({String})](#executeasyncrequest-{string})                                                                                                                                                                                       | Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.                                                                                                                                                                 |
| [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise)              | [executeAsyncRequest({String})](#executeasyncrequest-{string})                                                                                                                                                                                       | Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.                                                                                                                                                                 |
| void                                                                                        | [executeAsyncRequest(username, password, workstation, domain, successCallbackMethod, errorCallbackMethod)](#executeasyncrequest-username-password-workstation-domain-successcallbackmethod-errorcallbackmethod)                                      | Execute the request method asynchronous.                                                                                                                                                                                                                                                                  |
| void                                                                                        | [executeAsyncRequest(username, password, workstation, domain, successCallbackMethod, errorCallbackMethod, callbackExtraArgs)](#executeasyncrequest-username-password-workstation-domain-successcallbackmethod-errorcallbackmethod-callbackextraargs) | Execute the request method asynchronous using windows authentication.                                                                                                                                                                                                                                     |
| void                                                                                        | [executeAsyncRequest(username, password, successCallbackMethod, errorCallbackMethod)](#executeasyncrequest-username-password-successcallbackmethod-errorcallbackmethod)                                                                              | Execute the request method asynchronous.                                                                                                                                                                                                                                                                  |
| void                                                                                        | [executeAsyncRequest(username, password, successCallbackMethod, errorCallbackMethod, callbackExtraArgs)](#executeasyncrequest-username-password-successcallbackmethod-errorcallbackmethod-callbackextraargs)                                         | Execute the request method asynchronous using windows authentication.                                                                                                                                                                                                                                     |
| void                                                                                        | [executeAsyncRequest(successCallbackMethod, errorCallbackMethod)](#executeasyncrequest-successcallbackmethod-errorcallbackmethod)                                                                                                                    | Execute the request method asynchronous.                                                                                                                                                                                                                                                                  |
| void                                                                                        | [executeAsyncRequest(successCallbackMethod, errorCallbackMethod, callbackExtraArgs)](#executeasyncrequest-successcallbackmethod-errorcallbackmethod-callbackextraargs)                                                                               | Execute the request method asynchronous using windows authentication.                                                                                                                                                                                                                                     |
| [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) | [executeRequest()](#executerequest)                                                                                                                                                                                                                  | Execute the request method.                                                                                                                                                                                                                                                                               |
| [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) | [executeRequest(userName, password)](#executerequest-username-password)                                                                                                                                                                              | Execute the request method.                                                                                                                                                                                                                                                                               |
| [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) | [executeRequest(userName, password, workstation, domain)](#executerequest-username-password-workstation-domain)                                                                                                                                      | Execute a request method using windows authentication.                                                                                                                                                                                                                                                    |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                  | [getAllowedMethods(res)](#getallowedmethods-res)                                                                                                                                                                                                     | Returns the supported HTTP Request operations as a String Array                                                                                                                                                                                                                                           |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)                | [getHeaders()](#getheaders)                                                                                                                                                                                                                          | This returns a Object with header names as keys and arrays of values as values.                                                                                                                                                                                                                           |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [getHttpMethod()](#gethttpmethod)                                                                                                                                                                                                                    | Get the HTTP method of the request.                                                                                                                                                                                                                                                                       |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)                | [getQueryParameters()](#getqueryparameters)                                                                                                                                                                                                          | Get query parameters from the request URL.                                                                                                                                                                                                                                                                |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)                | [getRequestConfig()](#getrequestconfig)                                                                                                                                                                                                              | Get the Apache RequestConfig for this request.                                                                                                                                                                                                                                                            |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [getTimeout()](#gettimeout)                                                                                                                                                                                                                          | Get the configured response timeout in milliseconds.                                                                                                                                                                                                                                                      |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [getUrl()](#geturl)                                                                                                                                                                                                                                  | Get the URL of the request.                                                                                                                                                                                                                                                                               |
| void                                                                                        | [usePreemptiveAuthentication(b)](#usepreemptiveauthentication-b)                                                                                                                                                                                     | Whatever to use preemptive authentication (sending the credentials in the header, avoiding the server request to the client - useful when uploading files, as some http servers would cancel the first request from the client, if too big, as the authentication request to the client was not yet sent) |

## Methods Detailed

### addHeader(headerName, value)

Add a header to the request.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **headerName** ;
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **value** ;

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) A boolean indicating whether the header was successfully added to the request.

**Sample**

```js
method.addHeader('Content-type','text/xml; charset=ISO-8859-1')
```

### executeAsyncRequest()

Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.

This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the Promise is rejected with exception message as the value.

You can use Promise.all(\[promise1, promise2, promise3]) to wait for multiple promises to complete.\
A shortcut for this would be to use httpClient.executeRequest(requestArray) that returns 1 promise that is called when all of them are done.

**Returns:** [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise) A Promise that resolves with a Response object upon request completion or rejects with an error message if the request fails.

**Sample**

```js
request.executeAsyncRequest().then(response => { // handle the response }).catch(errorMessage => { // handle the error });})
```

### executeAsyncRequest({String})

Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.

This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the Promise is rejected with exception message as the value.

You can use Promise.all(\[promise1, promise2, promise3]) to wait for multiple promises to complete.\
A shortcut for this would be to use httpClient.executeRequest(requestArray) that returns 1 promise that is called when all of them are done.

**Parameters**

* [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) **{String}** password The password used for authentication.

**Returns:** [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise) A Promise that resolves with a Response object upon request completion or rejects with an error message if the request fails.

**Sample**

```js
request.executeAsyncRequest().then(response => { // handle the response }).catch(errorMessage => { // handle the error });})
```

### executeAsyncRequest({String})

Execute the request method asynchronous a Promise is returned which will be resolved or rejected when the request is completed or failed.

This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the Promise is rejected with exception message as the value.

You can use Promise.all(\[promise1, promise2, promise3]) to wait for multiple promises to complete.\
A shortcut for this would be to use httpClient.executeRequest(requestArray) that returns 1 promise that is called when all of them are done.

**Parameters**

* [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) **{String}** domain The domain to authenticate within.

**Returns:** [Promise](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/promise) A Promise that resolves with a Response object upon request completion or rejects with an error message if the request fails.

**Sample**

```js
request.executeAsyncRequest().then(response => { // handle the response }).catch(errorMessage => { // handle the error });})
```

### executeAsyncRequest(username, password, workstation, domain, successCallbackMethod, errorCallbackMethod)

Execute the request method asynchronous. Success callback method will be called when response is received.\
Response is sent as parameter in callback.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the errorCallbackMethod is called with exception message as parameter.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **username** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **workstation** The workstation the authentication request is originating from.
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **domain** The domain to authenticate within.
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out

**Returns:** void

**Sample**

```js
method.executeAsyncRequest('username','password','mycomputername','domain',globals.successCallback,globals.errorCallback)
```

### executeAsyncRequest(username, password, workstation, domain, successCallbackMethod, errorCallbackMethod, callbackExtraArgs)

Execute the request method asynchronous using windows authentication.\
Success callback method will be called when response is received. Response is sent as parameter in callback followed by any 'callbackExtraArgs' that were given.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out, network errors), the errorCallbackMethod is called with exception message as parameter followed by any 'callbackExtraArgs' that were given.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **username** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **workstation** The workstation the authentication request is originating from.
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **domain** The domain to authenticate within.
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out
* [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) **callbackExtraArgs** extra arguments that will be passed to the callback methods; can be used to identify from which request the response arrived when using the same callback method for multiple requests. Please use only simple JSON arguments (primitive types or array/objects of primitive types)

**Returns:** void

**Sample**

```js
method.executeAsyncRequest('username','password','mycomputername','domain',globals.successCallback,globals.errorCallback, [callIDInt])
```

### executeAsyncRequest(username, password, successCallbackMethod, errorCallbackMethod)

Execute the request method asynchronous. Success callback method will be called when response is received.\
Response is sent as parameter in callback.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the errorCallbackMethod is called with exception message as parameter.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **username** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out

**Returns:** void

**Sample**

```js
method.executeAsyncRequest(globals.successCallback,globals.errorCallback)
```

### executeAsyncRequest(username, password, successCallbackMethod, errorCallbackMethod, callbackExtraArgs)

Execute the request method asynchronous using windows authentication.\
Success callback method will be called when response is received. Response is sent as parameter in callback followed by any 'callbackExtraArgs' that were given.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out, network errors), the errorCallbackMethod is called with exception message as parameter followed by any 'callbackExtraArgs' that were given.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **username** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out
* [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) **callbackExtraArgs** extra arguments that will be passed to the callback methods; can be used to identify from which request the response arrived when using the same callback method for multiple requests. Please use only simple JSON arguments (primitive types or array/objects of primitive types)

**Returns:** void

**Sample**

```js
method.executeAsyncRequest(globals.successCallback,globals.errorCallback, [callIDInt])
```

### executeAsyncRequest(successCallbackMethod, errorCallbackMethod)

Execute the request method asynchronous. Success callback method will be called when response is received.\
Response is sent as parameter in callback.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out), the errorCallbackMethod is called with exception message as parameter.

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out

**Returns:** void

**Sample**

```js
method.executeAsyncRequest(globals.successCallback,globals.errorCallback)
```

### executeAsyncRequest(successCallbackMethod, errorCallbackMethod, callbackExtraArgs)

Execute the request method asynchronous using windows authentication.\
Success callback method will be called when response is received. Response is sent as parameter in callback followed by any 'callbackExtraArgs' that were given.\
This Response can be a response with a different status code then just 200, it could also be 500, which is still a valid response from the server, this won't go into the error callback.\
So you need to test the Reponse.getStatusCode() for that to know if everything did go OK.\
If no response is received (request errors out, network errors), the errorCallbackMethod is called with exception message as parameter followed by any 'callbackExtraArgs' that were given.

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **successCallbackMethod** callbackMethod to be called after response is received
* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **errorCallbackMethod** callbackMethod to be called if request errors out
* [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) **callbackExtraArgs** extra arguments that will be passed to the callback methods; can be used to identify from which request the response arrived when using the same callback method for multiple requests. Please use only simple JSON arguments (primitive types or array/objects of primitive types)

**Returns:** void

**Sample**

```js
method.executeAsyncRequest(globals.successCallback,globals.errorCallback, [callIDInt])
```

### executeRequest()

Execute the request method.

**Returns:** [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) A Response object containing the result of the executed HTTP request.

**Sample**

```js
var response = method.executeRequest()

To be able to reuse the client, the response must be
closed if the content is not read via getResponseBody
 or getMediaData:

response.close()
```

### executeRequest(userName, password)

Execute the request method.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **userName** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password

**Returns:** [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) A Response object containing the result of the HTTP request, authenticated with the provided username and password.

**Sample**

```js
var response = method.executeRequest()

To be able to reuse the client, the response must be
closed if the content is not read via getResponseBody
 or getMediaData:

response.close()
```

### executeRequest(userName, password, workstation, domain)

Execute a request method using windows authentication.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **userName** the user name
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **password** the password
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **workstation** The workstation the authentication request is originating from.
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **domain** The domain to authenticate within.

**Returns:** [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) A Response object containing the result of the HTTP request, authenticated using Windows authentication with the provided credentials.

**Sample**

```js
var response = method.executeRequest('username','password','mycomputername','domain');
```

### getAllowedMethods(res)

Returns the supported HTTP Request operations as a String Array

**Parameters**

* [Response](https://docs.servoy.com/reference/servoyextensions/server-plugins/http/response) **res** The response request to get the allowed methods from.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) An array of supported HTTP request operations for the given response.

**Sample**

```js
var supportedOperations = request.getAllowedMethods()
application.output(supportedOperations.join(','));
```

### getHeaders()

This returns a Object with header names as keys and arrays of values as values.

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) An Object with "name" > "value"

### getHttpMethod()

Get the HTTP method of the request.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The HTTP method (e.g., "GET", "POST").

### getQueryParameters()

Get query parameters from the request URL.

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) A map of parameter names to arrays of values.

### getRequestConfig()

Get the Apache RequestConfig for this request.

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) The built RequestConfig.

### getTimeout()

Get the configured response timeout in milliseconds.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) Timeout in ms, or –1 if none.

### getUrl()

Get the URL of the request.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The request URL.

### usePreemptiveAuthentication(b)

Whatever to use preemptive authentication (sending the credentials in the header, avoiding the server request to the client - useful when uploading files, as some http servers would cancel the first request from the client, if too big, as the authentication request to the client was not yet sent)

**Parameters**

* [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) **b** ;

**Returns:** void

***
