“标题”:“不支持的媒体类型”,“状态”:从 python 请求 API 时出现 415 错误

问题描述

为什么我在 react 和 java android 中使用相同的 restful api API 我没有收到错误 415 但是当我使用 python 请求时我收到一个错误:"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

解决方法

请尝试这样做。

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