"title":"Unsupported Media Type","status":415 error when request API from python

1.5k Views Asked by At

Why is the same restful api API when I use it with react and java android I don't get error 415 but when I use python requests I get an error: "title":"Unsupported Media Type","status":415

    URL = "http://localhost:44327/api/UserOfficial/UserOfficialAuthenticate"
    def Login():
    data = {
        'username': 'hienkieu',
        'password': '197353995'
    }
    length = len(json.dumps(data))
    hearder = {
        'accept': 'application/json',
        'content-Type' : 'application/json',
        'content-Length': length
    }
    r = requests.post(URL, data, hearder)
    return r.text
1

There are 1 best solutions below

12
On

Please try to do this.

URL = "http://localhost:44327/api/UserOfficial/UserOfficialAuthenticate"
def Login():
    data = {
        'sername': 'hienkieu',
        'password': '197353995'
    }
    data = json.dumps(data) # <<<<<<<<<////////////// Edit is here
    length = len(json.dumps(data))
    hearder = {
        'accept': 'application/json',
        'content-Type' : 'application/json',
        'host': '104.154.118.39',
        'content-Length': length
    }
    r = requests.post(URL, data, hearder)
    return r.text