HTTP GET request micropython

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
grumpypy
Posts: 2
Joined: Sun Oct 30, 2016 5:52 pm

HTTP GET request micropython

Post by grumpypy » Sun Oct 30, 2016 6:07 pm

Hello,
today i tried to program a get request via my esp8266.
I used the basic configuration of https://docs.micropython.org/ .
My aim is to send 2 values to a php-site on my rasppberry. I've got the fields name and age.
Before i came to micropython i played a lil bit with lua.
This command works so far:
...
conn:send("GET /get.php?name=444&age=555"
...

my structure in micropython look like this:

url = '192.168.1.12'
addr = socket.getaddrinfo(url, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send('GET /get.php name=444&age=555')

Sadly my webserver can not understand the pythonpart. Do you guys know what i have made wrong? How the send command has to look like correctly? Thanks so far

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: HTTP GET request micropython

Post by pfalcon » Sun Oct 30, 2016 6:16 pm

We have an example of a simple HTTP client: https://github.com/micropython/micropyt ... _client.py . By looking at it you should be able to tell what's wrong in your example (your HTTP request string is not conforming to HTTP standard).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: HTTP GET request micropython

Post by chrisgp » Sun Oct 30, 2016 6:34 pm

You might also want to consider trying out urequests (https://github.com/micropython/micropyt ... equests.py) instead of writing it yourself.

Once you get urequests.py on your filesystem so it can be imported it would simplify your code to the following (minus any typos I might've made):

Code: Select all

import urequests

response = urequests.get("http://192.168.1.12/get.php?name=444&age=555")
response.close()

grumpypy
Posts: 2
Joined: Sun Oct 30, 2016 5:52 pm

Re: HTTP GET request micropython

Post by grumpypy » Sun Oct 30, 2016 6:47 pm

Thank You very much! That's working! 8-)
But im still interested in the correct code of my example.
I tried s.send(b'GET /get.php HTTP/1.0\r\n\r\nname=1234&age=5678') and failed.

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: HTTP GET request micropython

Post by chrisgp » Sun Oct 30, 2016 11:20 pm

The first line of an HTTP request is the method (GET, POST, etc.) following by the path (including the query string) and finally the HTTP version. Altogether for your case that would be something like:

Code: Select all

s.send(b'GET /get.php?name=1234&age=5678 HTTP/1.0\r\n\r\n')
Your most recent example placed the parameters in the body portion of the request (the body starts after two consecutive \r\n newlines). This doesn't make sense for a GET.

Post Reply