Please enable JavaScript to view this site.

Navigation: Advanced topics > Programming topics > REST API Connections

HttpRequest class

Scroll Prev Next More

This class represents an Http request, which is all the data that is going to be sent to a web server. It is mainly used for interaction with REST API.

When creating a new request, specify a URL as a parameter:

 

$request = new HttpRequest( "https://service.com/api" );

Properties

method

string. HTTP request method.

default value: "GET"

"GET" and "POST" are the methods used in most cases.

See additional info here:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

$request->method = "POST";

 

url

string. The web page address. Should start with http:// or https://

May or may not include query parameters like

https://www.google.com?q=what+is+url

or

https://www.google.com

 

Code example:

$request->url = "https://www.api.com/api";

 

urlParams

 

Array. Parameters to be added to URL

Keys in the array are parameter names.

Values in the array are parameter values.

Neither keys nor values should be encoded for the web. Just specify them as they are.

$request->url = "https://www.api.com/api";
$request->urlParams["username"] = "my username";
$request->urlParams["password"] = "my password";

 

The resulting URI sent to the server will be:

https://www.api.com/api?username=my%20username&password=my%20password

 

headers

 

Array. HTTP headers to be added to the request

Keys in the array are header names.

Values in the array are header values.

Neither keys nor values should be encoded for the web.

Example:

// add Basic HTTP Authorization to the request
$request->headers["Authorization"] = "Basic " . base64_encode( $username . ':' .$password );

 

postPayload

 

Array. POST variables to be sent with the request

Keys in the array are parameter names.

Values in the array are parameter values.

Neither keys nor values should be encoded for the web.

$request->method = "POST";
$request->url = "https://www.api.com/api/cars/add";
$request->postPayload["make"] = "Tesla";
$request->postPayload["model"] = "Model 3";
 

postPayload parameter is completely ignored if body property is not null.

Notes:

POST values may be sent to the server in a number of ways. The HttpRequest class supports some of them. To make it use a specific format, specify the "Content-Type" header value. The following values are supported:

application/x-www-form-urlencoded - this is the most popular format. If your API receives data in POST requests and the manual says nothing about data format, this is it. HttpRequest class sends POST data in this format by default, and will add the Content-Type header itself if you don't specify it:

// this line is not necessary
$request->headers["Content-Type"] = "application/x-www-form-urlencoded";
$request->postPayload["make"] = "Tesla";
$request->postPayload["model"] = "Model 3";

application/json - when you API expects data in JSON format. If you specify Content-Type "application/json", the application will send the post data in JSON format.

multipart/form-data - another popular format. Set Content-Type header to "multipart/form-data" if your API requires it. The full format of Content-Type header also requires a "boundary" parameter, but you may omit it.

$request->headers["Content-Type"] = "multipart/form-data";

 

body

 

string. POST body to be sent with the request.

Default value: null

If body parameter is null, the application will use postPayload values to form request body.

Otherwise, if body has any other value, the postPayload property will be ignored, and body value will be sent in the request as is.

$request->body = "my custom request body";
// this line will be ignored
$request->postPayload["make"] = "Tesla";

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software