HTTP/S Calls
BoxLang makes it really easy to interact with any HTTP/S endpoint via the http
tag/construct. The http
call will generate an HTTP/S request and parse the response into a nice BoxLang structure.
You can use ANY http method in the http
calls, the default is a GET
operation.
As you can see from the example above, you can pass parameters to the HTTP request by using the child httpparam
construct. This parameter can be of many different types: header, body, xml, cgi, file, url, formfield, cookie
depending on the requirements of the http endpoint.
The Result Structure
The result structure will contain the following keys:
Key | Description |
---|---|
| The HTTP response code and reason string. |
| The body of the HTTP response. Usually a string, but could also be a Byte Array. |
| A structure of response headers, the keys are header names and the values are either the header value or an array of values if multiple headers with the same name exist. |
| An error message if applicable. |
| The mime type returned in the Content-Type response header. |
| A boolean indicateing if the response body is text or binary |
| The character set returned in the Content-Type header. |
| All the http response headers as a single string. |
HTTP Arguments
This construct accepts many arguments with different features you can use when executing http/s calls, below we list just the most common ones.
Argument | Type | Default | Description |
---|---|---|---|
| URL | The http/s endpoint to hit | |
| numeric | 80/443 | The port of the endpoint to hit. 80 for http and 443 for https |
| string | GET | The http method to use. |
| string | An optional server username | |
| string | An optional server password | |
| string | BoxLang | The user agent to simulate for the request |
| string | utf-8 | The encoding to use |
| boolean | false | No does not resolve URLs in the response body. As a result, any relative URL links in the response body do not work. Yes resolves URLs in the response body to absolute URLs, including the port number, so that links in a retrieved page remain functional. |
| boolean | true | If the response header includes a location field, determines whether to redirect execution to the URL specified in the field. |
| numeric | unlimited | A value in seconds of the max time to take for the request. |
| string | auto | If yes, convert to BoxLang binary type, No keep as text, auto let BoxLang detect and convert as necessary |
| string | http | The name of the variable you want the result structured returned into |
| boolean | false | Tells BoxLang to send all data specified by httpparam type="formField" tags as multipart form data, with a Content-Type of multipart/form-data. |
Basically, you can do any type of http/s calls and consume any type of RESTFul webservices with a nice BoxLang syntax!
HTTPParam
As mentioned before in our example we can use the httpparam
construct to pass parameters to the http/s endpoint. The parameters can be of different types as we can see in the following table.
Param Types
Type | Description |
---|---|
| Specifies an HTTP header. Does not URL encode the value |
| Specifies that the |
| Identifies the request as having a content-type of |
| Same as |
| Tells BoxLang to send the contents of the specified file. |
| Specifies a URL query string name-value pair to append to the http url attribute. URL encodes the value. |
| Specifies a form field to send. URL encodes the value by default. |
| Specifies a cookie to send as an HTTP header. URL encodes the value. |
Param Arguments
The available param arguments to the httpparam construct are:
Argument | Type | Default | Description |
---|---|---|---|
| string | The type of data from the available types above | |
| string | The variable name for the data | |
| string | The value of the variable | |
| path | Applies to | |
| boolean | false | Applies to |
| string | Applies to |
Here is another example for you:
Last updated