SORU
16 Mart 2012, Cuma


Post Python İstekleri kullanarak JSON

Bir sunucuya istemciden bir JSON POST ihtiyacım var. Python 2.7.1 ve simplejson kullanıyorum. İstemci İstekleri kullanıyor. Sunucu CherryPy. Sunucu (kod gösterilmez) kodlanmış JSON alabilirim, ama sunucuya bir JSON POST vermek istediğimde, "400 hatalı İstek".

Burada müvekkilim kodu:

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)

Burada sunucu kodu.

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())

Herhangi bir fikir?

CEVAP
31 Mart 2012, CUMARTESİ


Başlık bilgileri eksik çıktı. Aşağıdaki çalışır:

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)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Juan Carlos Candela Bordera

    Juan Carlos

    4 Mart 2009
  • Lena Danya

    Lena Danya

    11 NİSAN 2010
  • wolfys you tube

    wolfys you t

    22 Kasım 2006