Structure and Methods
Setup
To use the HTTP client in an XDK application, its header file has to be included in the source file as follows.
Before using the module for the first time, it also needs to be initialized by calling the following function.
GET
The next code show how to send a GET request for the resource “23.22.14.18:80/ip”
The central object here is the struct Msg_T. It contains all the information required to create HTTP request. The struct is created using HttpClient_initRequest(). Parameters are the address and port of the server that should receive the request. This call will trigger an assertion if no network connections is available.
HttpMsg_setReqMethod() is used to set the HTTP method of the request, in this case Http_Method_Get.
The last piece of information that needs to be set is the resource that we want to request. This can be done with the function HttpMsg_setReqUrl() that takes a simple c string as its argument.
An operation that is executed synchronously will block its thread until the operation is finished. However, this is an undesired behaviour in a real-time environment like the XDK. The other option is to perform an operation asynchronously, using callback functions that are called when the operation finished. This way, execution can continue immediately after initiating the operation.
The HttpClient_pushRequest() function, that is used to send a the HTTP request, takes two callback functions:
The first function (second parameter) is called when the request was successfully sent, or if an error occurred that prevented the system from sending the request.
The second function (third parameter) is called when the system received a response.
The following two code snippets provide sample implementations of both required callback functions. The first function simply validates the status code and prints an error message if something went wrong.
If no error occured and a response was received, the following function will print the response’s status code, content type and payload to the XDK Workbench console.
The Msg_T parameter that is passed to the response callback function is the same struct that was initially passed to the HttpClient_pushReques() function, only now containing the HTTP response data as well. The individual parts of the received response can be extracted using the functions HttpMsg_getStatusCode(), HttpMsg_getContentType()and HttpMsg_getContent(), with a pointer to the struct as argument. Since the response payload (if existing) is stored as a plain byte buffer in memory, HttpMsg_getContent() cannot simply return a reference to this buffer, but has to provide its length too. This is done using additional out parameters.
POST
The following code will create and send a POST request to “23.22.14.18:80/post”.
The code for the POST request looks very similar to the code for a GET request. There are only two differences:
The method is set to Http_Method_Post
Msg_prependPartFactory() is used to pass a function pointer in the Msg_T struct.
The HTTP API uses chunked transfer encoding for POST requests. The HTTP stack sends a new chunk whenever data is available. The pointer that is passed here to the message struct is a pointer to a functions that generates exactly these chunks. This function is called a part factory. It sequentially writes the payload into a handover buffer which then gets encoded and transferred to the server.
Set Host Header
The Host header is used to differentiate between multiple domains that run on a common IP. In this way, a single web server can receive requests that are addressed to different domains, and forward them to the corresponding virtual servers for processing.
The following code will set the host-header, using the Http API.
This can be directly copied to your sending-implementation and has to be called before HttpClient_pushRequest() and after HttpClient_initRequest().
Last updated
Was this helpful?