I'm trying to figure out how to implement my first RESTful interface using Django and django-rest-interface. I'm having problems with the HTTP PUT requests.
How do I access the parameters of the PUT request?
I thought they would be in the request.POST array, as PUT is somewhat similar to POST in my understanding, but that array is always empty.
What am I doing wrong?
Thanks for the help
request.POST processes form-encoded data into a dictionary, which only makes sense for web browser form submissions. There is no equivalent for PUT, as web browsers don't PUT forms; the data submitted could have any content type. You'll need to get the raw data out of request.raw_post_data, possibly check the content type, and process it however makes sense for your application.
More information in this thread.
if you figure in the dispatch of ResourceBase there are a line like:
elif request_method == 'PUT':
load_put_and_files(request)
return target.update(request, *args, **kwargs)
load_put_and_files let prepare for you the request.PUT with the data y the request.method is PUT, so you dont have to worry about that...
Related
I want to send information back-end via JS to update the database based on user response, but I don't need a JSON response or other HTTP rendering done.
It's a test type app so all I'm sending back is a single identifier (integer) and boolean value to indicate pass/fail on their response.
Currently I am using a fetch(url/${identifier}/bool) and all works as intended with the data management, but Python/Django throws an error as I am not returning any value.
So I have two questions.
Is fetch the right way of doing this as I am not actually expecting a response and I am doing no manipulation JS side with the data after calling it.
How can I stop Django from expecting a response on this as I intend to run the call as a simple function rather than an API call with JSON response sent back.
Thank you for the help.
Fixed the issue by simply using
return HttpResponse(status = 200)
I want to make a JSON request with the Python library requests where I only obtain certain JSON objects.
I know that it is really easy to process the JSON object obtained to only focus in the needed information, but that would throttle the request efficiency (in case it is done repeatedly).
As said, I know this is a possibility:
url = 'www.foo.com'
r = requests.get(url).json()
#Do something with r[3]['data4'], the only one who is going to be used.
But how could I directly only obtain r[3]['data4'] from the request?
Short Answer
To answer your question no, you can't but to understand why you need to know what is happening behind the scenes.
Behind the scenes
When you make a request such as r = requests.get('www.foo.bar') you are making a request to the server and you are viewing the result of that request when you do r.json(). This means that you cannot just get r[3]['data'] as you are parsing what the server sends to you unless the server only sends r[3]['data']. It may be possible to filter out everything else apart from that in the response processing but I am unaware of how to do it.
You can't, if the server does not allow it. If the target server allows you to specify fields you want then you can send that field list in your request and server will return you only those fields in JSON. Otherwise your will have to parse full JSON response and get your desired fields.
I'm wondering if there's a clever pattern for request-scoping arbitrary information without resorting to either TLS or putting the information in the session.
Really, this would be for contextual attributes that I'd like to not look up more than once in a request path, but which are tied to a request invocation and there's no good reason to let them thresh around in the session.
Something like a dict that's pinned to the request where I can shove things or lazy load them. I could write a wrapper for request and swap it out in a middleware, but I figured I'd check to see what best-practice might be here?
Just assign the dictionary directly to the request. You can do that in middleware or in your view, as you like.
Context processors. They are called once for every request and receive the actual request object - so you can add ANY data to the context, also based on the curent request!
How do I go about checking if a request is an ajax request in a controller method in Turbogears? Further, is it possible to return a 'partial' much like in rails or symfony if the request is an ajax request. I know about the json decorator but I need a way to return a partial of a mako template (because I need to format the data and don't want to to do it all in Javascript). For example if I want to return the formatted list for page two of a list of news stories, I do not want to return the entire page (but rather just the formatted list).
Thanks
jQuery, YUI, Prototype, Dojo, and MooTools all set the header X-Requested-With: XMLHttpRequest. You should be able to check for that header.
I'm trying to redirect/forward a Pylons request. The problem with using redirect_to is that form data gets dropped. I need to keep the POST form data intact as well as all request headers.
Is there a simple way to do this?
Receiving data from a POST depends on the web browser sending data along. When the web browser receives a redirect, it does not resend that data along. One solution would be to URL encode the data you want to keep and use that with a GET. In the worst case, you could always add the data you want to keep to the session and pass it that way.