开发者

Creating a nested JSON request with Python

A user needs to pass a json object as a part of the request. It would look something like this:

   {"token" :"ayaljltja",
   "addresses": [
      {'name':'Home','address':'20 Main Street', 
       '开发者_如何学运维city':'new-york'}, 
      {'name':'work', 'address':'x Street', 'city':'ohio'}
   ]}

I have two problems right now. First, I can't figure out how to test this code by recreating the nested POST. I can successfully POST a dict but posting the list of addresses within the JSON object is messing me up.

Simply using cURL, how might I do this? How might I do it with urrlib2?

My second issue is then deserializing the JSON POST object on the server side. I guess I just need to see a successful POST to determine the input (and then deserialize it with the json module).

Any tips?


First make sure your JSON is valid. Paste it into the JSONLint web page.

Currently your JSON has two issues:

  1. there is no comma between "token" :"ayaljltja" and "addresses": [...]
  2. a single quote is not a valid way of delimiting a JSON string, replace them all with double quotes.


With command line curl, save your JSON to a file, say data.json. Then try: curl -X POST -d @data.json http://your.service.url

It's also possible to enter the JSON directly to the -d parameter but (as it sounds like you know already) you have to get your quoting and escaping exactly correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜