Python 3 - POST request with urllib.request returning HTML data

I am trying to access a website API using a POST request, but it is returning HTML and I don't know why.

import urllib.request
url = 'https://matchbook.com'
loginReq = '"method": "/bpapi/rest/security/session", "params":{"username": "xxxxx","password": "xxxxx"}'

req = urllib.request.Request(url, loginReq.encode('utf-8'))
response = urllib.request.urlopen(req).read()
print (response)    

      

This returns an HTML stream that basically says that I am using an unsupported version of Internet Explorer and I should consider upgrading to a newer one.

I've spent some time searching the web and I am having a hard time finding details on get / post / put / delete requests in Python 3, most of the useful information I have found involves using the "requests" module in Python 2. Can the transition in Python 2 to make it easier?

Here is the information they provide in the documentation:

Resource URL: URL: matchbook.com URN: / bpapi / rest / security / session

Sample request: POST / security / session {"username": "j_henry", "password": " ** "}

Sample response: {"session token": "1418_1234567890", "user-id": 1418, "account": {// Same as the GET / account API response. ...}

+3


source to share


1 answer


The problem is almost certainly with the wrong data being sent to the API; as a variable loginReq.

You need to use a curl or some equivalent tool to determine exactly what parameters and values ​​need to be placed. Something along the lines



curl --data "method:/bpapi/rest/security/session&params={username=j_henry&password=**}" https://www.matchbook.com

      

Once you have your request down, you can start worrying about the actual programming of it.

0


source







All Articles