Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Content-Type in Django HttpResponse object for Shopify App

Tags:

I'm working on a Shopify app using Django, which I am hosting on a VPS with nginx and gunicorn.

I am trying to change the Content-Type of an HttpResponse object to application/liquid, so that I can use Shopify's application proxy feature, but it doesn't appear to be working.

Here is what I believe to be the relevant section of my code:

from django.shortcuts import render_to_response, render
from django.http import HttpResponse
from django.template import RequestContext
import shopify
from shopify_app.decorators import shop_login_required

def featured(request):
   response = HttpResponse()
   response['content_type'] = 'application/liquid; charset=utf-8'
   response['content'] = '<html>test123</html>'
   response['Content-Length'] = len(response.content)
   return response

According to the Django docs, I should set response[''content_type] in order to set Content-Type in the header. Unfortunately, when I go to the URL corresponding to this function in views.py, I get a 200 response but the Content-Type has not changed and Content-Length is 0. Here are my response headers:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 09 Jul 2013 12:26:59 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
X-Request-Id: 2170c81fb16d18fc9dc056780c6d92fd
content: <html>test123</html>
vary: Cookie
content_type: application/liquid; charset=utf-8
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"

If I change response['content_type'] to response['Content-Type'], I get the following headers:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 09 Jul 2013 12:34:09 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3097
Connection: keep-alive
X-Request-Id: 76e67e04b753294a3c37c5c160b42bcb
vary: Accept-Encoding
status: 200 OK
x-shopid: 2217942
x-request-id: 6e63ef3a27091c73a9e3fdaa03cc28cb
x-ua-compatible: IE=Edge,chrome=1
p3p: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
content-encoding: gzip
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"

Any ideas on how I can change the Content-Type of the response? Might this be a problem with my nginx or gunicorn configurations?

Thanks for your help!

like image 686
winter Avatar asked Jul 09 '13 12:07

winter


People also ask

What is HttpResponse in Django?

HttpResponse Methods – Django It is used to instantiate an HttpResponse object with the given page content and content type. HttpResponse.__setitem__(header, value) It is used to set the given header name to the given value.

What is request Meta in Django?

HttpRequest. META. A dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples: CONTENT_LENGTH – The length of the request body (as a string).

What kind of data is passed into a view in the request object?

The request object has view input field values in name/value pairs. When we create a submit button then the request type POST is created and calls the POST method. We have four data, those are in Name-Value pairs.

What is get and post method in Django?

GET and POSTDjango's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. GET , by contrast, bundles the submitted data into a string, and uses this to compose a URL.


3 Answers

Try the following:

def featured(request):     content = '<html>test123</html>'      response = HttpResponse(content, content_type='application/liquid')     response['Content-Length'] = len(content)      return response 

A quick tip, you could add this into the http or server block part of your NGINX configuration so you don't have to specify the encoding within views and other Django code:

charset utf-8; charset_types text/css application/json text/plain application/liquid; 
like image 125
Matt Deacalion Avatar answered Oct 21 '22 01:10

Matt Deacalion


So this worked for me:

def featured(request):   response = HttpResponse("", content_type="application/liquid; charset=utf-8")   response['Content-Length'] = len(content)   response.write('<html>test123</html>')   return response 

Thanks, everyone, for the help!

like image 25
winter Avatar answered Oct 21 '22 00:10

winter


Following the instructions from the docs it should be something like this:

# set content_type
response = HttpResponse("",
                        content_type="application/liquid; charset=utf-8")
# add content
response.write('<html>test123</html>')

Hope this helps!

like image 41
Paulo Bu Avatar answered Oct 20 '22 23:10

Paulo Bu