Arduino - HTTP Request
Arduino can play a role as a web client to make HTTP to a web server. Web Server can be a website, Web API or REST API, Web service ...
In this tutorial, we are going to learn:
- Basic knowledge of web client and web server
- How to make an HTTP request (GET and POST)
- How to send data to a web server (a website, WEB API or REST API)
- Arduino code:
- Make HTTP GET/POST request using Arduino Uno/Mega + Ethernet Shield 2
- Make HTTP GET/POST request using Arduino Uno/Mega + PHPoC Shield
- Make HTTP GET request and send data using Arduino Uno/Mega + Ethernet Shield 2
- Make HTTP GET request and send data using Arduino Uno/Mega + PHPoC Shield
- Make HTTP POST request and send data using Arduino Uno/Mega + Ethernet Shield 2
- Make HTTP POST request and send data using Arduino Uno/Mega + PHPoC Shield

The code for other WiFi or Ethernet Shield/Board are similar. The difference is only in library
Hardware Required
1 | × | Arduino UNO or Genuino UNO | |
1 | × | USB 2.0 cable type A/B | |
1 | × | Arduino Ethernet Shield 2 | |
1 | × | Ethernet Cable |
OR
1 | × | Arduino UNO or Genuino UNO | |
1 | × | USB 2.0 cable type A/B | |
1 | × | PHPoC WiFi/Ethernet Shield | |
1 | × | (optional)Ethernet Cable |
Basic Knowledge of Web Client and Web Server
When you access a website from your PC or smartphone, you just type the web address (URL) on a web browser, waits for a second, and then the webpage is displayed on your PC/smartphone. You do not know what your PC/smartphone does behind the screen. So, what happens behind the screen:
- Your PC/smartphone (as a web client) makes an HTTP request to a web server
- The web server returns an HTTP response to your PC/smartphone
- Your PC/smartphone receives the HTTP response and visualizes HTTP response on your screen
In this tutorial, We will make Arduino become a web client to do something similar to your PC/smartphone.
Web Address (URL)
An URL includes two parts: hostname and pathname. The hostname can be replaced by the IP address of the web server. For example: example.com/test
In HTTP GET request, URL can includes the query string. For example: example.com/test?temperature=20&humidity=70.
Query String
The query string is a set of name-value pairs, which are included in HTTP request to send data from web client to web server.
The name and value is separated by "=" character. The name-value pairs are separated by "&" character.
For example: temperature=26&humidity=70&state=2
HTTP Request
The HTTP request is composed of:
- HTTP request header
- (Optional) HTTP request body
HTTP request header and HTTP request body is seperated by two pair of a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
There are many request method. Among them, there are two popular methods: GET and POST.
Usually, we use GET method when we want to get data from web server, and use POST method to send data to web server. However, GET method can be used for either get and send data from/to webserver
Prerequisite
We need to determine the following values:
- Web Address (URL)
- Request method (POST or GET)
- HTTP port that web server uses (most of web server uses port 80 for HTTP)
- (Optional) Data to be sent to web server (query string). In this tutorial, we suppose that we send temperature and humidity to a web server with format ?temperature={t-value}&humidity={h-value}.
Web Address (URL) is splitted into hostname and pathname
How to Make an HTTP Request
This part presents only code related to HTTP. Full code for each shield will be presents in the last part.
- Declare request method, HTTP port, hostname, pathname, query string
- Declare a web client object
Arduino Ethernet Shield 2 | EthernetClient client; |
PHPoC WiFi/Ethernet Shield | PhpocClient client; |
Arduino Uno WiFi | WiFiClient client; |
- Connect to web server
- If connected to server, send HTTP request
- Read the response data from web server
How to Send Data to a Web Server
We can include data into HTTP request. data format is depended on HTTP request method:
- HTTP GET request
- Can only send data in query string format.
- Data is attached to the pathname.
- HTTP POST request
- Can send data NOT ONLY in query string format BUT ALSO any other format such as Json, XML, image ...
- Data is HTTP request body.
We just need to modify the code for sending the HTTP request:
- Build query string
- Modify the code for sending Data in the HTTP request
- HTTP GET: append query string to pathname
- HTTP POST: send query string as HTTP body
- Read the response data from web server
Complete Arduino Code for Making HTTP Request
The blow is the complete arduino code for making HTTP GET/POST request
Arduino HTTP GET/POST request using Arduino Ethernet Shield 2
Arduino HTTP GET/POST request using PHPoC Shields
Complete Arduino Code for Making HTTP GET Request with data
Arduino HTTP GET request with data using Arduino Ethernet Shield 2
Arduino HTTP GET request with data using PHPoC Shields
Complete Arduino Code for Making HTTP POST Request with data
Arduino HTTP POST request with data using Arduino Ethernet Shield 2
Arduino HTTP POST request with data using PHPoC Shields
※ NOTE THAT:
This tutorial used the dynamic IP address (via DHCP). If you want to use the static IP address:
- For Ethernet Shield 2, you need to modify Arduino code. See Arduino Ethernet Shield 2 with static IP address
- For PHPoC Shield, you need just need to change the IP setting via web. See Arduino - PHPoC Shield
The Best Arduino Starter Kit
See Also
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.