Python 请求:Success/Error 未调用

Python Requests: Success/Error Not Called

正在尝试使用 python 请求。当我尝试将从 url 收到的响应保存到变量中然后 post 它变回一个不同的 url。我有什么办法可以解决这个问题吗?

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

您必须使用您取回的实际代币:

根据主页上的说明:

================================
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

最后一部分实际上是反转你从上次 post:

得到的字符串
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