5

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?

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Jeremy, thanks. My json is actually valid in real life. Just a typo in SO. When I look at the Request Object, the issue is that the "addresses" object looks like the following: [u"{'city': 'new-york', 'name': 'Home', 'display_value': '2 Main Street'}", u"{'city': 'new-york', 'name': 'Home', 'display_value': '2 Main Street'}"]. I don't know why i'm getting unicode objects in my list. When I try to serialize those objects, the serializer fails (because of the single quotes, I believe).
2

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.

2 Comments

Hey Ian, I was afraid of something like that. Quoting/escaping is a pain that I'm aware of here. Any thoughts on using urllib2 or urllib to save me from some of this annoyance? I couldn't figure it out with urlencode.
stackoverflow.com/questions/4348061/… looks like it covers what you're trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.