2

我想用 django 调用一个使用 POST 的 RESTful api。我知道如何进行 GET,但如何进行 POST?

为了得到,我使用requests.get(...)

API调用是:

curl -v -X POST -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -X POST \
     --user user:password \
     https://this.is.an.external.domain \
     -d "{\"name\": \"Marcus0.1\",\"start\": 500000,\"end\": 1361640526000}"

更新

所以,我找到了requests.post,但是如何翻译上面的curl命令

4

1 回答 1

2

转换为 Python Requests 调用的 curl 命令将是:

# construct a python dictionary to serialize to JSON later
item = { "name": "Marcus0.1", "start": 500000, "end": 1361640526000 }

resp = requests.post("https://this.is.an.external.domain", 
              data=json.dumps(item),  # serialize the dictionary from above into json
              headers={
                       "Content-Type":"application/json",
                       "Accept": "application/json"
                      })

print resp.status_code
print resp.content
于 2013-08-06T21:46:58.833 回答