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

2
  • Looks like your question has been answered (in the question) here: stackoverflow.com/questions/9958039/… Commented Aug 6, 2013 at 17:09
  • This has nothing to do with django. It's a basic question about requests. Commented Aug 6, 2013 at 18:50

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I think you're missing the authentication. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.