Django REST Framework POST with file error (using ModelResource)

I have a really big problem sending data with a file in a Django REST Framework application. I created a simple application using the example on the djangorestframework website. So I have a urls file:

class MyImageResource(ModelResource): model = Image 

and in urlpatters:

 url(r'^image/$', ListOrCreateModelView.as_view(resource=MyImageResource)), url(r'^image/(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MyImageResource)), 

The image model is simple:

 class Image(models.Model): image = models.ImageField(upload_to=get_file_path) name = models.CharField(max_length=256, blank=True) description = models.TextField(blank=True) 

Testing a REST page in a browser works fine. Even placing data with files.

My problem is that I want to create a simple python application to publish data. I used simple urllib2, but I get 500 internal errors or 400 Bad Request:

 poza = open('poza.jpg', 'rb') initial_data = ( {'name', 'Imagine de test REST'}, {'description', 'Dude, this is awesome'}, {'image', poza}, ) d = urllib.urlencode(initial_data) r = urllib2.Request('http://localhost:8000/api/image/', data=d, headers={'Content-Type':'multipart/form-data'}) resp = urllib2.urlopen(r) code = resp.getcode() data = resp.read() 

I also tried using MultipartPostHandler:

 import MultipartPostHandler, urllib2, cookielib cookies = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler) params = { "name":"bob", "description":"riviera", "content" : open("poza.jpg", "rb") } opener.open("http://localhost:8000/api/image/", params) 

but the same thing: 500 or 400 errors, and the server (python manage.py runningerver) stops with the following errors:

 Exception happened during processing of request from ('127.0.0.1', 64879) Traceback (most recent call last): File "C:\Python27\lib\SocketServer.py", line 284, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 310, in process_request self.finish_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 323, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 570 , in __init__ BaseHTTPRequestHandler.__init__(self, *args, **kwargs) File "C:\Python27\lib\SocketServer.py", line 641, in __init__ self.finish() File "C:\Python27\lib\SocketServer.py", line 694, in finish self.wfile.flush() File "C:\Python27\lib\socket.py", line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) error: [Errno 10053] An established connection was aborted by the software in yo ur host machine 

If anyone has, please give me an example of publishing data with files or tell me what is wrong with my python zip code. I could not find more examples.

The server looks fine, I can receive POST data in the browser. Thank you very much.

+4
source share
1 answer

Doesn't look like you are reading in a file and passing a pointer to the file instead?

Try:

 initial_data = ( {'name', 'Imagine de test REST'}, {'description', 'Dude, this is awesome'}, {'image', poza.read()}, ) 
+1
source

All Articles