简体   繁体   中英

python post request error

I am trying to generate authentication token from "belfrics.in" api

    import json
    from urllib.request import Request, urlopen
    from urllib.parse import urlencode
    import requests

    belfrics_url1 = "https://india-exchange.belfrics.com:443/gateway/public/authenticate"
    belfrics_data1 = {'username': 'abc', 'password': 'xyz', 'stayLoggedIn': True}
    belfrics_headers = {'Content-Type': 'application/json'}
    belfrics_req1 = requests.post(belfrics_url1,params=belfrics_data1, headers=belfrics_headers)
    print(belfrics_req1.text)

Everytime I get this response

{"success":false,"missing_authenticators":[],"infos":[],"warnings":[],"errors":[{"JSON_DECODE_FAILED":["No error"]}],"results":{}}

I am not able to figure why I get JSON_DECODE_FAILED error. I even tried encoding the params by using urlencode but still got the same error. I replaced

params=belfrics_data1

with

params=urlencode(belfrics_data1)

ok so I found the solution by changing

params=belfrics_data1

to

json=belfrics_data1

Can someone explain why that worked?

The API documentation is here : https://india-exchange.belfrics.com/api#!/common.json/authenticate

Your site expects JSON; by using the json parameter instead of the params parameter, requests encoded belfries_data1 into JSON for you, making the site happy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM