【问题标题】:"title":"Unsupported Media Type","status":415 error when request API from python“标题”:“不支持的媒体类型”,“状态”:从 python 请求 API 时出现 415 错误
【发布时间】:2021-08-23 03:51:47
【问题描述】:

为什么当我将它与 react 和 java android 一起使用时相同的 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

【问题讨论】:

    标签: python http-status-code-415


    【解决方案1】:

    请尝试这样做。

    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
    

    【讨论】:

    • 您正在使用 Django_rest_framework API 吗?或者您正在使用简单的 API?
    • 我使用简单的 API。我的 Android 应用和 Frontend react 仍然正常使用
    • 尝试从您的请求中删除标头...它应该可以工作。在我看来,没有必要传递这些标头
    • 我听从了你的建议。但是还是不行。
    • 一个最大的解决方案,使用 django_rest_framework。
    猜你喜欢
    • 2017-10-23
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多