Django POST curl REST api



我想用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命令

转换为Python请求调用的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

相关内容

  • 没有找到相关文章

最新更新