# HTTPRequest

The **HTTPRequest** object lets you easily send an asynchronous HTTP request to a remote webserver and receive and process the response.

This object is created via the [Dialog](dialog) object's `NewHTTPReq` method, and so requires a [Script Dialog](../../../scripting/script_dialogs) in order to use. The dialog also must be [detached](../../../scripting/script_dialogs/the_dialog_message_loop/detached_dialogs).

The general process to use this object is as follows:

1. Create the HTTPRequest object
2. Add any required headers
3. Add POST data or GET query name/value pairs as required
4. Send the request
5. Process "http" events in your message loop
6. Read any returned data when you get a "data" event.

| Property Name | Return Type | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| contenttype   | _string_    | When a response has been received, returns the value of the Content-Type response header.                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| complete      | _bool_      | Returns True when a request is complete (and all data has been received).                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| errorcode     | _int_       | Returns the numerical error code, if any.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| errortext     | _string_    | Returns the error message, if any.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| id            | _int_       | Returns the ID of the request. This is assigned automatically when the request is created. You'll need this if you use more than one request at once.                                                                                                                                                                                                                                                                                                                                                                                               |
| response      | _string_    | Returns the HTTP response message.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| responsecode  | _int_       | Returns the HTTP response code (e.g. 200, 404).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| status        | _string_    | <p>Returns the current status of the request. This is also provided via the <a href="msg"><strong>Msg</strong></a><strong>.value</strong> property when you retrieve an HTTP message in your message loop. Valid status values are: notready, ready, pending, data, complete, error, shutdown.</p><p>A status value of "data" means the response has been received and data is available to read. A status of "complete" means the response is complete (e.g. a HEAD request won't return any data, so you'd look for "complete" in that case).</p> |

| Method Name        | **Arguments**                                                                                                                                                                     | Return Type                                                                     | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Abort              | _none_                                                                                                                                                                            | _none_                                                                          | <p>Aborts the request if it hasn't yet returned any data, and removes any headers and post data that you've added.</p><p>Calling this method resets the <code>HTTPRequest</code> object to its initial state, meaning it can be reused to send another request.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| AddHeader          | <p>&#x3C;string:name><br>&#x3C;string:value><br><em>or</em><br>&#x3C;<strong>Map</strong>:headers></p>                                                                            | _none_                                                                          | Adds one or more headers to the HTTP request. You can either provide two strings (a name and a value), or a [Map](map) of name -> value pairs to add more than one header at once.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| AddPostData        | <p>&#x3C;string:name><br>&#x3C;value><br>[&#x3C;string:contenttype>]<br>[&#x3C;string:filename>]<br>[&#x3C;string::encoding>] <em>or</em><br>&#x3C;<strong>Map</strong>:data></p> | _none_                                                                          | <p>Adds one or more name/value pairs to the request's POST data. You can either provide the name and value as two arguments, in which case you can also optionally provide a content type string. Alternatively, you can provide a <a href="map">Map</a> of name -> value pairs. The <em>value</em> of a posted data element can either be a string, or a <a href="blob">Blob</a> object to provide binary data.</p><p>If you only add a single item of POST data you can leave the name empty (pass an empty string). This lets you, for example, POST data formatted as JSON - set the <em>name</em> to an empty string, the <em>value</em> to the JSON data in string form, and <em>contenttype</em> to <code>application/json</code>.</p><p>If adding a <strong>Blob</strong> you can provide the <em>filename</em> argument which gives the receiving system a suggested filename to store the file under. You can also specify the <em>encoding</em> type - set to "base64" to have the <strong>Blob</strong> sent as base64-encoded data (otherwise by default it will be sent as binary). If you've encoded the data yourself you can specify another encoding type which will be passed as the value of the <em>Content-Transfer-Encoding</em> header.</p><p>The data will be sent as <code>multipart/form-data</code> if you either pass a <strong>Blob</strong> object, or the <strong>Map</strong> contains more than one name/value pair.</p> |
| AddQueryData       | <p>&#x3C;string:name><br>&#x3C;string:value><br><em>or</em><br>&#x3C;<strong>Map</strong>:data></p>                                                                               | _none_                                                                          | Adds one or more name/value pairs to the request's QUERY data. You can either provide the name and value as two arguments, or you can provide a [Map](map) of name -> value pairs. The query data will be automatically url-encoded and appended to the request string when the request is sent.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| GetResponseHeaders | _none_                                                                                                                                                                            | **Map**                                                                         | Returns a [Map](map) of name -> value pairs containing the response headers once the response data has been received.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ReadResponse       | _none_                                                                                                                                                                            | <p><em>string</em> or<br><strong>Image</strong> or<br><strong>Blob</strong></p> | <p>Once a response has been received (i.e. your message loop gets an "http" message with the value type set to "data"), you can call this function to read the returned data. Note that this function will block until all data is received.</p><p>The function tries to interpret the returned data automatically based on the Content-Type header in the response. Text data wil be converted to a string if possible. Image data will be returned as <a href="image">Image</a> object if it's a format that Opus recognises. Otherwise, a <a href="blob">Blob</a> containing the response data will be returned.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ReadResponseData   | \[\<int:size>]                                                                                                                                                                    | **Blob**                                                                        | <p>Use this function instead of <code>ReadResponse</code> to read the raw data from the response without trying to interpret it. The data is returned as a <a href="blob">Blob</a> object.</p><p>You can specify the maximum about of data to read in bytes. If the requested size is available it will be returned; otherwise the function will return immediately with as much data as possible. If you specify <em>size</em> as 0, or don't specify a size, then the function will block until the entire response has been received. If you specify <em>size</em> as -1 then the function will return as much data as possible, but won't block.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| SendRequest        | <p>&#x3C;string:url><br>[&#x3C;string:action>]</p>                                                                                                                                | _none_                                                                          | <p>Sends the HTTP request to the specified webserver. The <em>url</em> parameter should be the full URL to retrieve (e.g. <code>https://www.gpsoft.com.au</code>). If you've added any query parameters via the <code>AddQueryData</code> method these will be appended automatically.</p><p>By default HTTP action will be chosen automatically based on the state of the object - <code>POST</code> if <code>AddPostData</code> has been used, otherwise <code>GET</code>. You can override this with the optional <em>action</em> parameter if you like - e.g. specify <code>HEAD</code> to just retrieve the headers without any response data.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| SetTimeout         | \<int:seconds>                                                                                                                                                                    | _none_                                                                          | Specifies a timeout in seconds (default is 30). Functions which block like `ReadResponse` will timeout if a complete response is not received in time. Set to 0 for no timeout.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| SetUserAgent       | \<string:agent>                                                                                                                                                                   | _none_                                                                          | Specifies the User-Agent string of the request, if requires.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| Shutdown           | _none_                                                                                                                                                                            | _none_                                                                          | <p>Call this to close down the request and free any associated resources. This aborts the request if it's still outstanding. Once you've called this function you can't query the request for any other data except the <code>complete</code> property.</p><p>You don't need to call this function but if you use a lot of requests you may want to once they're completed in order to free up memory.</p><p>Once <code>Shutdown</code> has been called the request object can no longer be used. Note that messages from the request may still be in your dialog's message queue even after calling <code>Shutdown</code> - best practice is to check the <code>complete</code> property before using any other methods or properties.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
