Use of httpResponse django - django

As far as I can think of, HttpResponse is used for REST application. I cannot figure out any other use of httpresponse. I want to understand other uses of HttpResponse. Help by anyone would be very appreciated. Thanks in advance.

HttpResponse is used to send an HTTP response of any kind and can be used for any application, not just a REST app.
I'm sure you've used the render function a lot in your views to return a template. Under the hood, it returns an HttpResponse. Here's the source code.
Another use case is what if you want to send an image or a PDF document from your views? You can't use render for that. Here's a example:
def pdf_view(request):
response = HttpResponse(content_type="application/pdf")
with open('my_file.pdf', 'rb') as f:
response.write(f.read())
return response
In short, HttpResponse can be used to send any kind of data over HTTP. You can use it to set custom headers, or anything you can think of related to HTTP responses.

Related

How to set http response code in Parse Server cloud function?

A parse server cloud function is defined via
Parse.Cloud.define("hello", function(request, response) {..});
on the response, I can call response.success(X) and response.error(Y), and that sets the http response code and the body of the response.
But how do I define a different code, like created (201)?
And how do I set the headers of the response?
thanks, Tim
You are allowed to return any valid JSON from response.success(). Therefore, you could create an object with fields such as code, message, and value, so you can set the code, give it a string descriptor, and pass back the value you normally would, if there is one. This seems to accomplish what you need, though you will have to keep track of those codes across your platforms. I recommend looking up standard http response codes and make sure you don't overlap with any standards.

Can't see JSON response in JMeter View Results Tree

I was wondering if someone could tell me why I can't see the json response from a request that is also of type json in the JMeter View Results Tree. I know that the response is there because I use a regular expression to extract data (I wrote the reg ex based on result returned in Firebug). So the data can be extracted from the json response it just doesn't display in View Result Tree. I can see the json response for another request for a different app I performance test with JMeter, but that particular request is an ajax request. not sure
Add a sample writer and display content here.
http://jmeter.apache.org/usermanual/component_reference.html#Simple_Data_Writer
You will see the issue
HI there any one who looks in this post for jmeter, you may be bit worried when you cant find answers in internet regarding how to log your response when you sent a request , here is the solution and simple as it is...
just add a line like below.
result.setResponseData("put any content here to be displayed as String.", "UTF-8");
and check your response will be cool as you done.

HttpResponse vs. Render

I was looking over some code and came to this question -- Django: What is the difference b/w HttpResponse vs HttpResponseRedirect vs render_to_response -- which discusses the different types of request responses.
Is there ever a reason to use HttpResponse over render? If so, what would be the use case and advantage of doing so? Thank you.
render is used to for what the name already indicate: to render a template file (mostly html, but could be any format). render is basically a simple wrapper around a HttpResponse which renders a template, though as said in the previous answer you can use HttpResponse to return others things as well in the response, not just rendering templates.
Sure, say you're making an AJAX call and want to return a JSON object:
return HttpResponse(jsonObj, mimetype='application/json')
The accepted answer in the original question alluded to this method.

Does HttpPost only returns xml and not json?

I've been working on a project that sends data to the server and receives a return value of JSON data. Unfortunately when I try to use HttpPost it returns as xml and not JSON.
I was able to fix the problem by using HttpGet.
I am just curious if it is possible to return JSON data using HttpPost? Can anyone enlightened me? I've been Googling around for a while but still unable to find any answers to my question.
P.S.
Please don't flag my question.
The problem is not your HttpPost or HttpGet, the key point here is your server. When it receives a POST it is programmed to return a XML-like string and if it gets a GET it will return a JSON-like string
If it is a third-party server, make sure you set the corresponding parameters for the format you would like to retrieve. Typically this is the Accept header. If it is your own server, zozelfelfo is right and it depends on your implementation, but not on the HTTP verb.

How can I enable GZIP compression of the JSON response entity on Reslet?

I have a Restlet application already working that accepts JSON and returns JSON entity as response.
I'm trying to understand how I can compress the JSON entity that is returned in the response.
I did not find any clear example on how to achieve it.
I think I have to put somewhere on the router chain the Encoder/EncoderService classes, but I really don't understand where and how to use them.
Could anybody help me?
After some testing, I got the answer.
Creating a new filter like this
Filter encoder = new Encoder(getContext(), false, true, new EncoderService(true));
inside the createInboundRoot() method of my own Application class did the trick, the client requests were already containing the gzip header needed.

Resources