0

I am trying to make a post request in python and I believe I am doing everything correct. However it is not returning any response. I can't seem to figure out if there is anything wrong with my request. It seems like there may be something wrong with the service if I am not getting any response back. Is there anything inherently wrong with what I've written here?

import json
import urllib2

data = {'first_name': 'John','last_name': 'Smith','email': '[email protected]','phone': '215-555-1212'}

req = urllib2.Request('https://someurl.io/')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))
print response.read()
print response.headers
1
  • 2
    Not really a direct answer to your question, but using urllib2 is a pain. Try requests. I think you'll like it. It's generally a much simpler API. Eliminates a lot of boiler plate. Even has a nice post method. Commented Feb 19, 2015 at 0:20

1 Answer 1

2

Honestly, unless you love urllib2, I suggest using requests. Here's the same data code in requests:

import requests
import json

payload = {'first_name': 'John','last_name': 'Smith','email': '[email protected]','phone': '215-555-1212'}
url = 'https://someurl.io/'

r = requests.post(url, json=json.dumps(payload))

print r.content
print r.headers
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.