Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST Framework POST data with file error (using ModelResource)

I have a really big problem trying to post data with file in a Django REST Framework app. I've created a simple application by the example at djangorestframework website. So I have the 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 the REST page in browser, works perfect. Even posting data with files.

My problem is that I want to create a simple python application to post data. I've used simple urllib2, but I get 500 Internal Error 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've tried also with 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 same: 500 or 400 errors and the server (python manage.py runserver) 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 somebody have, please give me an example of posting data with files or tell me what's wrong in my posting python code. I couldn't find any more examples.

The server looks ok, I can POST data in browser. Thank you very much.

like image 247
trd Avatar asked Nov 13 '22 00:11

trd


1 Answers

It doesn't look as if you are reading in the file, and are instead passing the file pointer?

Try:

initial_data = (    
    {'name', 'Imagine de test REST'},
    {'description', 'Dude, this is awesome'},
    {'image', poza.read()},
)
like image 56
mfitzp Avatar answered Dec 28 '22 06:12

mfitzp