Trouble making HTTP request

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
tylerw
Posts: 1
Joined: Sun Mar 14, 2021 11:45 pm

Trouble making HTTP request

Post by tylerw » Mon Mar 15, 2021 12:25 am

Hello all,

I'm working on a project which uses an ESP8266 to access the Halo Public API to download game history data which could then be processed and displayed with a seven segment display. However I am have trouble accessing the Halo public API. Using python on my desktop computer I was able to access the API using the request library. When I tried to do the same thing on the ESP8266 using the urequest library I get an error regarding 'INVALID_HANDSHAKE'. I do not have much experience with networking so I am unsure what this means exactly.

Here is the actual error:

Code: Select all

Traceback (most recent call last):
  File "/usr/bin/ampy", line 8, in <module>
    sys.exit(cli())
  File "/usr/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/ampy/cli.py", line 338, in run
    output = board_files.run(local_file, not no_output, not no_output)
  File "/usr/lib/python3.9/site-packages/ampy/files.py", line 309, in run
    self._pyboard.execfile(filename, stream_output=True)
  File "/usr/lib/python3.9/site-packages/ampy/pyboard.py", line 285, in execfile
    return self.exec_(pyfile, stream_output=stream_output)
  File "/usr/lib/python3.9/site-packages/ampy/pyboard.py", line 279, in exec_
    raise PyboardError('exception', ret, ret_err)
ampy.pyboard.PyboardError: ('exception', b"network config: ('192.168.1.203', '255.255.255.0', '192.168.1.1', '192.168.1.1')\r\nMaking an API Call\r\n", b'Traceback (most recent call last):\r\n  File "<stdin>", line 86, in <module>\r\n  File "<stdin>", line 79, in run\r\n  File "<stdin>", line 34, in callAPI\r\n  File "urequests.py", line 108, in get\r\n  File "urequests.py", line 60, in request\r\nOSError: (-260, \'INVALID_HANDSHAKE\')\r\n')
After getting nowhere with google searches, I moved on to try using the usocket library to make an HTTP request.
Here is the code I used for that:

Code: Select all

 def getHaloData():
    # Connect to the host. 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    addr = socket.getaddrinfo('www.haloapi.com', 80)[0][-1]
    client.connect(addr)

    # Request data from the host.
    path = 'https://www.haloapi.com/stats/h5/players/EnduroCat14/matches HTTP/1.1'
    host = 'www.haloapi.com'
    key = '••••••••••••••••••••••••••••••••'
    client.send(bytes('GET %s\r\nHost: %s\r\nOcp-Apim-Subscription-Key: %s\r\n\r\n' % (path, host, key), 'utf8'))
    
    while True:
        data = client.recv(4096)
        if data:
            print(str(data, 'utf8'))
        else:
            break
    client.close()
Unfortunately that did not work either. Here is what getHaloData() prints out:

Code: Select all

HTTP/1.1 404 Resource Not Found
Content-Length: 54
Content-Type: application/json
Date: Sun, 14 Mar 2021 23:38:27 GMT

{ "statusCode": 404, "message": "Resource not found" }
At this point, I have no idea what is going wrong. All I'm looking for is some sign that I am connecting to the API. I'm nearly certain that the HTTP request is right. This is the request that is given to me from the Halo API website:

Code: Select all

GET https://www.haloapi.com/stats/h5/players/EnduroCat14/matches HTTP/1.1
Host: www.haloapi.com
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
I followed the ESP8266 setup tutorial from micropython.org and was able to run all the examples just fine, so I'm mostly confident that the board is setup correctly. I was even able to make an HTTP request to google using an example I found online.

Any help would be greatly appreciated. I would love to use the ESP8266 and micropython to power my project, but I am pretty stuck. Thanks in advance!

User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: Trouble making HTTP request

Post by ioukos » Tue Mar 23, 2021 9:23 pm

Hello,

Can you please post your full original urequest code ?

BR

Post Reply