views:

52

answers:

1

My Django view generates a PDF via pycairo in response to a POST (I'm not redirecting in response to the POST). When I POST using desktop browsers I can save and/or view the PDF using Adobe Reader or Document Viewer. However, when I POST via my android browsers the Adobe PDF Reader and the ThinkFree viewers both report the file as corrupted.

Looking in the logfiles on my appserver both Android browsers are sending a POST followed immediately by a GET for the same page:

[24/Sep/2010:22:49:20 -0500] "POST /courses/blank/create/ HTTP/1.1" 200 8895 "http://example.com/create/" "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; SPH-D700 Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"

[24/Sep/2010:22:49:20 -0500] "GET /courses/blank/create/ HTTP/1.1" 200 9432 "-" "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; SPH-D700 Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"

When I look at the file saved to the phone it is the HTML of the GET request.

Here's a skeleton of the view:

def create(request, template="blankgrids/create.html"):
    if request.method == 'POST':
        form = BlankGridForm(request.POST)
        if form.is_valid():
            response = HttpResponse(mimetype='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=example.pdf'
            # snipped pycairo code that writes directly into response
            return response
    else:
        form = BlankGridForm()

    return render_to_response(template,
                          {'form': form},
                          context_instance=RequestContext(request))

So I'm wondering what could be causing the android browsers to perform the GET?

A: 

I don't know what's making it do the GET, but you may not be able to change its behavior. You could change your code so that the POST returned a redirect to a GET for the PDF. If you have data in the POST needed to create the PDF, you could either store it in the session or add it as url parameters, possibly with a security token to prevent replays and so on.

Ned Batchelder
Ned - Thanks for the suggestion. In this site the generated PDFs can be shared and not regenerated. So I was thinking of having the POST check if the PDF exists. If not it generates it to disk. Either way the POST redirects to a permanent URL for the PDF. This allows for bookmarking by users and caching by the web server. It is weird that these mobile browsers have this behavior...
saschwarz
Sounds perfect.
Ned Batchelder