【问题标题】:Python Requests Code 141 ErrorPython 请求代码 141 错误
【发布时间】:2015-01-05 05:39:59
【问题描述】:

我正在尝试使用 python 中的请求将 json 字典发布到 url。我需要从 url 中取回一个字符串,但我不断收到代码 141 错误 -{"code":141,"error":"Missing a github repository link"}。我正在使用这个网站(http://docs.python-requests.org/en/latest/user/quickstart/) 来做请求。

关于为什么我不断收到该错误的任何想法?代码如下。

import requests
import json

payload = { "email" : "jade@gmail.com", "github" : "https://github.com/"}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/register", params = payload, headers = headers)

print(r.url)
print r.text

更新:该建议有效,但现在当我尝试将从 url 收到的响应保存到变量,然后将其发布回不同的 url。

#Store the token into a variable
token = r.text

payload = { "token" : token}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/getstring", json = payload, headers = headers)

print r.text

【问题讨论】:

    标签: python python-requests


    【解决方案1】:

    由于您正在发出POST 请求和you need to provide a JSON in the request body,请使用json argument,而不是params

    r = requests.post("http://challenge.code2040.org/api/register", 
                      json=payload, 
                      headers=headers)
    

    (已测试 - 取回了一个令牌)

    请注意,json 参数是在 requests 2.4.2 中引入的。

    【讨论】:

    • 我要发布的网址返回一个带有两个键的字典,例如说它们被称为“result1”和“result2” 我怎样才能打印出“result2”的值? Print r.text 打印出所有的值。
    • @user2466886 使用r.json() 方法获取python字典。
    • 谢谢,但我收到了错误,{"code":141,"error":"success/error was not called"}。下面的代码: token = r.text payload = { "token" : token} headers = {'content-type': 'application/json', "Accept": 'application/json'} r = requests.post("@ 987654324@", json = 有效负载,headers = headers) print r.text
    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2018-01-08
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多