Python请求:未调用成功/错误

时间:2015-01-05 00:01:11

标签: python python-requests

尝试使用python请求。我得到一个{“代码”:141,“错误”:“成功/错误未被调用”}错误当我尝试将我从网址收到的响应保存到变量中然后将其发布回不同的网址。我有什么想法可以解决这个问题吗?

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

#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

1 个答案:

答案 0 :(得分:0)

你必须使用你得到的实际代币:

根据主页的说明:

================================
Stage I: Reverse a string
================================

Once you’re registered, it’s time to get started on the challenges.

The first one is straightforward. You’re going to reverse a string.

That is, if the API says “cupcake,” you’re going to send back “ekacpuc.”

POST a JSON dictionary with the key token and your previous token value to this endpoint:

http://challenge.code2040.org/api/getstring

The getstring endpoint will return a string that your code should then reverse, as in the example above.

Once that string is reversed, send it back to us. POST some JSON to:

http://challenge.code2040.org/api/validatestring

Use the key token for your token.
Use the key string for your reversed string.

第1部分输出令牌:

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", json = payload, headers = headers)

#Store the token into a variable
token = r.json()  # {u'result': u'ZO8FFqVjWp'}

第2部分获取字符串:

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

jsn =  r.json() # {"result":"Wv55g"} # we need to reverse that string

最后一部分实际上正在颠倒你从我们上一篇文章中得到的字符串:

payload = {"token" :token["result"], "string" :jsn["result"][::-1]} # jsn["result"][::-1]} reverse the string 
r = requests.post("http://challenge.code2040.org/api/validatestring", json = payload, headers = headers)

print(r.json())

{u'result': u'PASS: stage1. Enrollment record updated!'} # all good!

您的地址r

还有一个额外的"http://challenge.code2040.org/r" <- should not be there