簡體   English   中英

request.post 不支持的媒體類型錯誤

[英]Unsupported Media type error for request.post

我正在使用 python 進行 api 調用。 這里我有我試圖訪問的網站中生成的 json 格式的參數。 但是當我嘗試運行該程序時,我收到415: unsupported Media Type錯誤。 不確定我做錯了什么,因為我使用的是網站生成的參數。

到目前為止,這是我的代碼

def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)


url = 'https://einv-apisandbox.nic.in/gstvital/api/auth'

parameters = {
  "header": {
    "ClientID": "TheClientIDGoesHere",
    "ClientSecret": "TheClientSecretGoesHere"
  },

  "data": {
    "UserName": "Username",
    "Password": "Password",
    "AppKey": "AppKey",
    "ForceRefreshAccessToken": "false"
  }
}

response = requests.post(url, params=parameters)

jprint(response.json())

在上面的代碼中,我刪除了實際參數並用虛擬文本替換它們。 但是當我用實際參數嘗試它們時,我收到以下錯誤

{
    "status": 415,
    "title": "Unsupported Media Type",
    "traceId": "|df46105a-49e1b43f80675626.",
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13"
}

我改變的一件事是這個代碼"ForceRefreshAccessToken": "false" 在生成的 json 代碼中, false不在引號內

不知道我做錯了什么。 請幫我。

import requests
import json


def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)


url = 'https://einv-apisandbox.nic.in/gstvital/api/auth'

parameters = {
    "header": {
        "ClientID": "TheClientIDGoesHere",
        "ClientSecret": "TheClientSecretGoesHere"
    },

    "data": {
        "UserName": "Username",
        "Password": "Password",
        "AppKey": "AppKey",
        "ForceRefreshAccessToken": False
    }
}

hdr = {"Content-Type": "application/json"}

response = requests.post(url, data=parameters, headers=hdr)

print(response.status_code)

print(response.json())

錯誤 415 表示站點不支持該媒體類型。 這可以通過在標頭中明確聲明內容類型為 JSON 來解決。 hdr = {"Content-Type": "application/json"}來自站點的響應代碼是“200:OK”,因此您的請求有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM