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!
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.
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).
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.
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.
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;
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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With