In this tutorial we will check how to send a HTTP POST request using the UART OBLOQ. For a tutorial on how to send a HTTP GET request, please check here.
We are going to send the request to a testing fake online REST API, like we did in the previous tutorial. Note that although this API supports POST requests and returns a success message, no new resource will be created in the backend server since the API is just for testing.
As we have been doing, we will use a serial to USB converter and the Arduino IDE serial monitor to interact with the OBLOQ. You can check here how to perform the connections.
Since we will need to connect the OBLOQ to a WiFi network in order to be able to reach the remote server, you can check in more detail here the command needed to perform the connection.
As mentioned, the first thing we need to do is connecting the UART OBLOQ to a WiFi network. This is achieved with the following command:
The expected result is shown in figure 1. As can be seen, after the connection is established with success, OBLOQ should return the IP assigned to it on the network.
Figure 1 – Connection of OBLOQ to a WiFi network.
Now that the device is connected to a WiFi network, it is able to send HTTP POST requests to a server. We will send the request to this endpoint. You can check at figure 2 the expected result by sending a POST request to the mentioned endpoint with Postman.
Note that since the API was designed for testing, it will always accept the body of our request, no matter how it is formatted. So we don’t need to worry about following any particular structure or protocol. The answer from the server is a 201 HTTP code (which corresponds to “resource created”), and it returns a JSON body with the ID of the created resource.
As mentioned, the resource will not be effectively created in the backend, so multiple POST requests to this endpoint will always return the same ID, and a subsequent GET request will not have the resource with ID 101 (the one that is always returned after the POST).
Figure 2 – Testing POST request to fake online API with Postman.
To send the HTTP POST request from OBLOQ, we need to send the following command, changing destinationURL with the API endpoint and postBody with the body of the request:
As mentioned, the testing API will always accept the body, so we can send something we want. You can check at figure 3 an example in the Arduino IDE serial monitor, before sending the command to OBLOQ.
Figure 3 – Sending the POST request to the testing API.
The expected result is shown in figure 4. Note that we get the 201 code and the same JSON structure with the ID of the new resource, as we did using Postman.
Figure 4 – Answer to the POST request.
Note that the command answer follows the same format of a GET request, like covered in the previous tutorial: