How to send a POST request as JSON?

data = { 'ids': [12, 3, 4, 5, 6 , ...] } urllib2.urlopen("http://abc.com/api/posts/create",urllib.urlencode(data)) 

I want to send a POST request, but one of the fields should be a list of numbers. How can i do this? (JSON?)

+96
json python post url
Mar 17 2018-12-12T00:
source share
6 answers

If your server expects the POST request to be json, you need to add a header as well as serialize the data for your request ...

Python 2.x

 import json import urllib2 data = { 'ids': [12, 3, 4, 5, 6] } req = urllib2.Request('http://example.com/api/posts/create') req.add_header('Content-Type', 'application/json') response = urllib2.urlopen(req, json.dumps(data)) 

Python 3.x

stack overflow




If you do not specify a title, this will be the default type application/x-www-form-urlencoded .

+147
Mar 17 '12 at 1:19
source share

I recommend using the incredible requests module.

http://docs.python-requests.org/en/v0.10.7/user/quickstart/#custom-headers

 url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'content-type': 'application/json'} response = requests.post(url, data=json.dumps(payload), headers=headers) 
+105
Mar 17 2018-12-12T00:
source share

for Python 3.4.2 I found the following will work:

 import urllib.request import json body = {'ids': [12, 14, 50]} myurl = "http://www.testmycode.com" req = urllib.request.Request(myurl) req.add_header('Content-Type', 'application/json; charset=utf-8') jsondata = json.dumps(body) jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes req.add_header('Content-Length', len(jsondataasbytes)) print (jsondataasbytes) response = urllib.request.urlopen(req, jsondataasbytes) 
+54
Nov 11 '14 at 23:07
source share

This works great for Python 3.5 if the URL contains a Query String / Parameter value,

Request URL = https://bah2.com/ws/rest/v1/concept/
Parameter Value = 21f6bb43-98a1-419d-8f0c-8133669e40ca

 import requests url = 'https://bahbah2.com/ws/rest/v1/concept/21f6bb43-98a1-419d-8f0c-8133669e40ca' data = {"name": "Value"} r = requests.post(url, auth=('username', 'password'), verify=False, json=data) print(r.status_code) 
+16
13 Oct '16 at 9:34
source share

You need to add a header, or you will get an http 400 error. The code works well on python2.6, centos5.4

the code:

  import urllib2,json url = 'http://www.google.com/someservice' postdata = {'key':'value'} req = urllib2.Request(url) req.add_header('Content-Type','application/json') data = json.dumps(postdata) response = urllib2.urlopen(req,data) 
+4
Feb 24 '14 at
source share

Here is an example of how to use the urllib.request object from the Python standard library.

 import urllib.request import json from pprint import pprint url = "https://app.close.com/hackwithus/3d63efa04a08a9e0/" values = { "first_name": "Vlad", "last_name": "Bezden", "urls": [ "https://twitter.com/VladBezden", "https://github.com/vlad-bezden", ], } headers = { "Content-Type": "application/json", "Accept": "application/json", } data = json.dumps(values).encode("utf-8") pprint(data) try: req = urllib.request.Request(url, data, headers) with urllib.request.urlopen(req) as f: res = f.read() pprint(res.decode()) except Exception as e: pprint(e) 
+1
Aug 08 '19 at 14:24
source share



All Articles