2

I want to call a RESTful api with django that uses a POST. I know how to do a GET, but how do I do a POST?

For the get, I use requests.get(...)

the API call is:

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}"

update

So, I found requests.post, but how do translate the above curl command

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
Marcus
  • 9,032
  • 11
  • 45
  • 84
  • Looks like your question has been answered (in the question) here: http://stackoverflow.com/questions/9958039/using-python-requests-library – George Stocker Aug 06 '13 at 17:09
  • This has nothing to do with django. It's a basic question about `requests`. – vad Aug 06 '13 at 18:50

1 Answers1

2

That curl command translated to a Python Requests call would be:

# 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
John Sheehan
  • 77,456
  • 30
  • 160
  • 194