A simple HTTP client to make GET and POST requests
Posted: Thu Apr 09, 2015 8:53 am
Hello everyone,
Let's say your ESP8266 acquires data from a sensor. You want to periodically send it on a remote webserver for logging, on a shared host for instance.
The easiest solution is to POST data on HTTP (like a form on a web page would).
Here is a simple library that will do the HTTP requests for you:
https://github.com/Caerbannog/esphttpclient
Installing
If your project looks like esphttpd from Sprite_tm:
Now append lib/esphttpclient to the following Makefile line and you should be ready:
Example GET
Include httpclient.h from user_main.c then add this line to perform a single request and display your public IP address.
The output looks like this. It is a success because the 200 code means OK. You can now write your own function to replace http_callback_example.
Example POST
To send sensor data use http_post as follows. If you are not interested in the response you can pass NULL for the callback function.
Pull requests are welcome.
Let's say your ESP8266 acquires data from a sensor. You want to periodically send it on a remote webserver for logging, on a shared host for instance.
The easiest solution is to POST data on HTTP (like a form on a web page would).
Here is a simple library that will do the HTTP requests for you:
https://github.com/Caerbannog/esphttpclient
- Easy to use.
- Supports multiple requests in parallel.
- Supports GET and POST requests.
- Tested with Espressif SDK v1.0.0
Installing
If your project looks like esphttpd from Sprite_tm:
Code: Select all
git clone http://git.spritesserver.nl/esphttpd.git/
cd esphttpd
git submodule add https://github.com/Caerbannog/esphttpclient.git lib/esphttpclient
git submodule update --init
Now append lib/esphttpclient to the following Makefile line and you should be ready:
Code: Select all
MODULES = driver user lib/esphttpclient
Example GET
Include httpclient.h from user_main.c then add this line to perform a single request and display your public IP address.
Code: Select all
http_get("http://wtfismyip.com/text", "", http_callback_example);
The output looks like this. It is a success because the 200 code means OK. You can now write your own function to replace http_callback_example.
Code: Select all
http_status=200
strlen(full_response)=244
body_size=15
body=208.97.177.124
<EOF>
Example POST
To send sensor data use http_post as follows. If you are not interested in the response you can pass NULL for the callback function.
Code: Select all
http_post("http://httpbin.org/post", "first_word=hello&second_word=world", "", http_callback_example);
Pull requests are welcome.