Micropython http ip name formatting

Hello,

I am able to perform some http requests and get back data from web sites with simple host names. But I can’t seem to get it to work with more complicated hostnames. Is this because it doesn’t like the “/” and or “?” inside the hostname? Thank you

For example, using these addresses in the s.send line:
api.geoiplookup.net - works
api.geoiplookup.net/?query=xxx.xxx.xxx.xxx does not work
freegeoip.live/json does not work
freegeoip.live/xml does not work

Is there a way I can format the hostname to work within micropython?

This code works, and gives back country and lat/lon info for my Portenta using wifi

addr = socket.getaddrinfo('api.geoiplookup.net', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: api.geoiplookup.net\r\n\r\n')
data = s.recv(1000)
print (data)

Below does not work and gets an error “400 bad request”

addr = socket.getaddrinfo('freegeoip.live', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: freegeoip.live/json/\r\n\r\n')
data = s.recv(1000)
print (data)

I think you need to format the headers correctly to pass these args, I don’t know how you should search for a solution with Python test it on PC, then try it on the cam. You could also try using the urequests library (it’s builtin you can just import it) openmv/urequests.py at master · openmv/openmv · GitHub

EDIT:

You could also use Wireshark and see how Python (or any browser) formats the request, and just duplicate it.