urllib2 and json

0 votes
I am trying to figure out how to do a POST request using urllib2 with the data being in JSON format. Can anyone help me with it?
Mar 27, 2019 in Python by ana1504.k
• 7,910 points
1,776 views

1 answer to this question.

0 votes
I think you need to specify a content-type header if you want it to really work. Here you include a content-type header:

import json
import urllib2
data = json.dumps([1, 2, 3])
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
answered Mar 27, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to Creat a JSON response using Django and Python?

Hello @kartik, I usually use a dictionary, not ...READ MORE

answered Aug 6, 2020 in Python by Niroj
• 82,840 points
3,143 views
0 votes
1 answer

Parse and restructure key:value pair of JSON Object to Python Object

@Khushboo, Can you link to a correctly formatted ...READ MORE

answered Oct 1, 2020 in Python by Gitika
• 65,770 points
2,157 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,443 views
0 votes
1 answer
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,300 points
929 views
0 votes
2 answers

difference between class and instance attributes

Apart from the performance, there is a ...READ MORE

answered Sep 17, 2018 in Python by SDeb
• 13,300 points
887 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP