Am nevoie pentru a POSTA un JSON de la un client la un server. Am'm folosind Python 2.7.1 și simplejson. Clientul este folosind Cereri. Serverul este CherryPy. Pot OBȚINE un hard-coded JSON de la server (codul nu apare pe site), dar cand incerc sa postez un JSON la server, am "400 Bad Request".
Aici este codul meu client:
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)
Aici este codul de server.
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
def POST(self):
self.content = simplejson.loads(cherrypy.request.body.read())
Orice idei?
Ca de Cereri versiunea 2.4.2 și înainte, puteți folosi alternativ 'json' parametru în apelul pe care îl face mai simplu.
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
EDIT: Această caracteristică a fost adăugat la documentația oficială. Puteți vizualiza aici: Cereri documentația
Se pare că a fost lipsă de informații de antet. Următoarele lucrări:
url = "http://localhost:8080"
data = {'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
De cereri 2.4.2 (https://pypi.python.org/pypi/requests), "json" parametru este acceptată. Nu este nevoie să specificați "Content-Type". Deci varianta mai scurta:
requests.post('http://httpbin.org/post', json={'test': 'cheers'})
Funcționează perfect cu python 3.5+
client:
import requests
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
r = requests.post("http://localhost:8080", json={'json_payload': data})
server:
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def POST(self):
self.content = cherrypy.request.json
return {'status': 'success', 'message': 'updated'}
Aceasta funcționează perfect pentru Python Versiunea 3.5, Dacă URL-ul conține Șirul de Interogare / Parametru valoare,
Cerere URL = https://baaaah2.com/ws/rest/v1/concept/
Parametru valoare = 21f6bb43
import requests
headers = {'Content-type': 'application/json'}
result = requests.post('https://baaaah2.com/ws/rest/v1/concept/21f6bb43',auth=('username', 'password'),verify=False, headers=headers)
print(result.status_code)