My Adventures in Coding

November 21, 2009

Django HTTP PUT with XML payload in a test

Filed under: Django — Brian @ 8:04 pm
Tags: , , , ,

I am new to Django. I have only worked on two small projects in Django so far, but I am definitely enjoying the experience!

The problem I had recently was that I wanted to create a test for a new HTTP PUT call I was about to write. When creating a PUT call in Django using JSON, writing a test and using the call is very easy. However, this time I was working on a service that allows for a GET and a PUT call with data in an XML format. I had some trouble getting the payload set up correctly, so I thought I would share the solution that worked for me:

data = get_testdata_from_file(filename=XML_TEST_FILE)
payload = {'wsgi.input':FakePayload(data), 'CONTENT_LENGTH':len(data)}
self.client.put(TEST_REST_URL, **payload)

I am sure this is trivial for you experienced Django developers, but if you are a rookie I hope this saves you some time!


Create a free website or blog at