简体   繁体   English

request.post 不支持的媒体类型错误

[英]Unsupported Media type error for request.post

I am working on a api call with python.我正在使用 python 进行 api 调用。 Here I have the parameters in json format that was generated in the website I am trying to access.这里我有我试图访问的网站中生成的 json 格式的参数。 But when I try to run the program I get an 415: unsupported Media Type error.但是当我尝试运行该程序时,我收到415: unsupported Media Type错误。 Not sure what I am doing wrong, as I am using the parameters generated by the website.不确定我做错了什么,因为我使用的是网站生成的参数。

this is my code so far到目前为止,这是我的代码

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())

In the above code, I have removed the actual parameters and replaced them with dummy text.在上面的代码中,我删除了实际参数并用虚拟文本替换它们。 But when I try them with the actual parameters I get the following error但是当我用实际参数尝试它们时,我收到以下错误

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

One thing I changed was this code "ForceRefreshAccessToken": "false" .我改变的一件事是这个代码"ForceRefreshAccessToken": "false" In the generated json code, the false was not inside quotes在生成的 json 代码中, false不在引号内

Not sure what I am doing wrong.不知道我做错了什么。 Please help me.请帮我。

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())

Error 415 indicates that the media type is not supported by the site.错误 415 表示站点不支持该媒体类型。 This can be fixed by explicitly stating in the header that the content-type will be JSON.这可以通过在标头中明确声明内容类型为 JSON 来解决。 hdr = {"Content-Type": "application/json"} The response code from the site is "200:OK", therefore your request works. hdr = {"Content-Type": "application/json"}来自站点的响应代码是“200:OK”,因此您的请求有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM