【问题标题】:Django x-www-form-urlencoded requestDjango x-www-form-urlencoded 请求
【发布时间】:2017-04-02 17:57:37
【问题描述】:

我正在尝试使用 django 执行以下请求:

我尝试了以下代码,但它不起作用:

data = {'username': admin, 
        'password': 123, 
        'grant_type': 'password',
        'client_id': 'xxxx',
        'client_secret': 'xxxx'}
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=data, headers=headers)

感谢您的帮助!

【问题讨论】:

    标签: django


    【解决方案1】:

    默认是表单编码的。

    通常,您希望发送一些表单编码的数据——很像 HTML 形式。为此,只需将字典传递给 data 参数。您的 数据字典将在请求时自动进行表单编码 制作完成。

    >>> payload = {'key1': 'value1', 'key2': 'value2'}
    >>> r = requests.post("http://httpbin.org/post", data=payload)
    >>> print r.text
    {
      "origin": "179.13.100.4",
      "files": {},
      "form": {
        "key2": "value2",
        "key1": "value1"
      },
      "url": "http://httpbin.org/post",
      "args": {},
      "headers": {
        "Content-Length": "23",
        "Accept-Encoding": "identity, deflate, compress, gzip",
        "Accept": "*/*",
        "User-Agent": "python-requests/0.8.0",
        "Host": "127.0.0.1:7077",
        "Content-Type": "application/x-www-form-urlencoded"
      },
      "data": ""
    }
    

    http://docs.python-requests.org/en/v0.10.7/user/quickstart/#make-a-post-request

    【讨论】:

    • 谢谢它很好用。所以现在请求已正确执行,但执行请求后出现以下错误:{'error': 'unsupported_grant_type'}
    • 根据我阅读的内容,如果数据未使用标头 {"content-type": "application/x-www-form-urlencoded"} 发送,则会返回此错误。但是我发送了这个请求的标头没有?
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多