python - 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”,因此您的请求有效。
推荐阅读
- azure - 如何在 Azure Web App 上获取 SSL 的静态 IP 地址
- linux - 如何在没有后续目录的情况下压缩目录中的文件?
- angular - 在 Angular NGXS 中取消订阅
- objective-c - Cocoa获取用户所有登录项的列表和路径
- ios - Crashlytics 未收到来自 TestFlight 的崩溃
- javascript - js_exception TypeError search.load 不是函数
- c++ - 通过使用模板/别名对通用和静态向量类使用相同的测试主
- vba - 如何从重量以太网秤读取重量?
- opencv - Opencv triangulatePoints 函数没有给出世界单位的距离
- powerbi - 将单行员工记录转换为透视 Sankey 布局