Jon
Jon

Reputation: 3502

Correct Django URL config to read JSON

I'm trying to learn a bit of REST. I have added several views to an existing Django app to try and do various things using REST and JSON. I am able to get my app to send out requested data through several views, but I can't seem to get it to accept JSON as part of the URL.

I have created a view that looks like this:

def restCreateEvent(request, key, jsonString): errors = checkKey(key)

if errors == None:
    eventJson = json.loads(jsonString)

    eventInfo = eventJson['event']
    title = eventInfo['title']
    description = eventInfo['description']

    locationInfo = eventInfo['location']

    place = locationInfo['place_name']
    street = locationInfo['street_address']
    city = locationInfo['city']
    state = locationInfo['state']
    zip = locationInfo['zip']

    event = models.Event()
    event.title = title
    event.description = description
    event.street_address = street
    event.place_name = place
    event.city = city
    event.state = state
    event.zip = zip
    event.save()

else:
    return errors

However, I can;t seem to get the URL correct, here is what I have now:

(r'^events/rest/create/(?P<key>\d+)/(?P<jsonString>.+)', 'events.views.restCreateEvent')

When I attempt to access the following url, Django debug complains that none of my urls match it.

http://127.0.0.1:8000/events/rest/33456/create/{"title":"test","description":"this is a    test","location":{"place_name":"somewhere","street_address":"123   main","city":"pittsburgh","state":"pa","zip":"11111"}}

Right now the view is never called, so obviously my url is wrong. So, is my approach totally wrong here? If not how do I fix the url?

Upvotes: 0

Views: 330

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599926

Why would you do this? The way to send JSON, like any payload, is to put it in the POST data, not the URL.

Upvotes: 0

Related Questions