How to POST image to django rest API - django

I am not too practiced in handling image files, and I am very confused about posting and handling an image using my django REST API. I am using a python script as client just for testing (will be ios app eventually).
Anyone who can help me receive an image file, and save it locally on the server would be greatly appreciated.
Client:
i = Image.open('image.jpeg')
upload = {
'id': 3,
'picture': i
}
r = requests.post(ip, data=upload, stream=True)
print(r.text)
Server:
class Picture_Upload(APIView):
def post(self, request):
f = request.data['picture']
return Response(f.content)

You might want to use MultiPartParser to parse the form data so that you can read it on your APIView, and surely that we have to send data as multipart/form-data. Like so:
upload = {
'picture': open('image.jpeg', 'rb')
}
data = {
'id': 3
}
resp = requests.post('/upload_url/', files=upload, data=data)
and your view:
class Picture_Upload(APIView):
parser_classes = (MultiPartParser, FormParser, )
def post(self, request):
picture = request.data['picture']
id = request.data['id']
# Use your model to handle the uploaded file
return Response(status=200)

Related

"The submitted data was not a file. Check the encoding type on the form." validation error, despite uploading correct file (django rest framework)

I'm trying to create endpoint for uploading images, in my api, which i'm building with django rest framework.
When I try to test the endpoint with postman, i'm getting response
"image": [
"The submitted data was not a file. Check the encoding type on the form."
]
with status code 400.
When I try to print variable with image to console I get
[<TemporaryUploadedFile: test.jpg (image/jpeg)>]
I've checked out some tutorials and I think i'm sending the file correctly.
That is my post man configuration
that's the view
class ImageView(APIView):
parser_classes = (MultiPartParser, )
permission_classes = (IsAuthenticated, IsRestaurant)
def post(self, request, *args, **kwargs):
data = {
'image': request.data.pop('image'),
'info': request.user.info.pk
}
file_serializer = RestaurantImageSerializer(data=data)
if file_serializer.is_valid():
file_serializer.save()
return Response(status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
the serializer
class RestaurantImageSerializer(serializers.ModelSerializer):
class Meta:
model = RestaurantImage
fields = '__all__'
and model
class RestaurantImage(models.Model):
info = models.ForeignKey(RestaurantInfo, related_name='images', on_delete=models.CASCADE)
image = models.ImageField()
def __str__(self):
return self.image.name
Your Postman configuration could be an issue, try removing all the wrong or unnecessary headers. I think you need only Authorization in your case. This also could help you out: https://stackoverflow.com/a/41435972/4907382

How to pass data saved from a POST method to the GET method using REST API Django (without a model)?

I have created an API that allows me to upload an image using the POST method in POSTMAN. After submission, I want to display that image name after making a GET request. I am not using any model and I don't intend to grab the image from the directory it is stored in; since I will be uploading images in a server later.
I have looked at multiple sources. A few examples are this, and this.
This is my current code so far but not successful:
views.py:
class API(APIView):
parser_classes = (MultiPartParser,)
def get(self, request, *args, **kwargs):
name = self.request.GET.get('image')
if name:
return Response({"img_name": name}, status=200)
return Response({"img_name" : None}, status = 400)
def post(self, request):
file = self.request.data
img_file = file['image'] #store the image data in this variable
if img_file:
uploaded_file = img_file
img = [{"image_name": uploaded_file}]
serializer = ImgSerializer(img, many = True).data
return Response(serializer, status = 200)
else:
return Response("Please upload", status = 400)
serializers.py:
from rest_framework import serializers
class ImgSerializer(serializers.Serializer):
image_name = serializers.CharField()
My expected result within GET request should be like this:
{'image_name' : 'image_name_from_POST_Request'}
But I am getting this result instead:
None
How can I pass data from the POST request to the GET request using Django's rest framework? Is there an efficient way to deploy this requirement without using a model?
I figured it out. I just created a JSON file in the POST method and stored the necessary data in it. Finally, in order to view the data within the GET method, I opened the file and returned it as a Response.
views.py:
class API(APIView):
parser_classes = (MultiPartParser,)
def get(self, request):
with open('data.txt') as json_file:
data = json.load(json_file)
if data:
return Response(data, status=200)
return Response({"name" : None}, status = 400)
def post(self, request):
posted_file = self.request.data
img_file = posted_file['image']
if img_file:
uploaded_file = img_file
data = [{"image_name": uploaded_file}]
json_data = {"image_name": uploaded_file}
data = {}
data['key'] = []
data['key'].append(json_data)
with open('data.txt', 'w') as outfile:
json.dump(image, outfile)
serializer = ImgSerializer(image, many = True).data
return Response(serializer, status = 200)
else:
return Response(serializer.errors, status = 400)

How to make a POST request to the end point, in views in django for chatterbot?

i am new to django!
I want to make a chatterbot chatbot in my website, for which i need to make a POST request in views. I have created a model. I am using mysql database for this.
I have visited github and other website and finally got a code, but it doesn't have the POST request
this is my models.py:
class Response(models.Model):
statement = models.ForeignKey(
'Statement',
related_name='in_response_to',
on_delete=False
)
response = models.ForeignKey(
'Statement',
related_name='+',
on_delete=False
)
unique_together = (('statement', 'response'),)
occurrence = models.PositiveIntegerField(default=0)
def __str__(self):
s = self.statement.text if len(self.statement.text) <= 20 else self.statement.text[:17] + '...'
s += ' => '
s += self.response.text if len(self.response.text) <= 40 else self.response.text[:37] + '...'
return s
this is where i need to make a POST request in views.py
def post(self, request, *args, **kwargs):
response = Response.objects.all()
if request.is_ajax():
input_data = json.loads(request.read().decode('utf-8'))
else:
input_data = json.loads(request.body.decode('utf-8'))
self.validate(input_data)
response_data = self.chatterbot.get_response(input_data)
return JsonResponse(response, response_data, status=200)
def get(self, request, *args, **kwargs):
data = {
'detail': 'You should make a POST request to this endpoint.',
'name': self.chatterbot.name,
'recent_statements': self._serialize_recent_statements()
}
# Return a method not allowed response
return JsonResponse(data, status=405)
If you're using django rest framework (DRF), i recommend you start by doing QuickStart and then Serialization steps. DRF has a really good documentation and in the Serialization you could find how to make a POST request by defining:
Models
Serializers
Api
Routers

Using django rest framework to send an image

I want to use DRF to send an image to response to a certain url, for this purpose I have written these codes:
#Renderer class
class ImageRenderer(renderers.BaseRenderer):
media_type = 'image/png'
format = 'image'
def render(self, data, media_type=None, renderer_context=None):
return data
#view class
class ShowImage(APIView):
renderer_classes = (ImageRenderer,)
def get(self, request, format=None):
print ('format', format)
if format == 'image':
image_file = open('path_to_image', 'rb')
response = HttpResponse(image_file, content_type='image/png')
response['Content-Disposition'] = 'attachment; filename={}'.format('image_filename')
#urls.py
urlpatterns = format_suffix_patterns([
url(r'image/?$', views.ShowImage.as_view())
])
But my problem is that always the input format is None although the request.accepted_media_type shows me image/png
I tried these requests using httpie:
http -vj 127.0.0.1:8000/api/image Accept:image/png
http -vj 127.0.0.1:8000/api/image.image Accept:image/png
But I could not get the format I expected. What is my mistake in using DRF?

Retrieving images from GridFS using django-tastypie-mongoengine

I have a project in Django, and I'm using mongoengine to save images into a Mongo database using GridFSStorage.
All ok so far, but the problem is... when trying to retrieve the images via http request, with a REST API made with django-tastypie-mongoengine, I get back a json object like this:
{"file": "<GridFSProxy: 516ed7cf56ba7d01eb09f522>", "id": "516ed7cf56ba7d01eb09f524", "resource_uri": "/api/v1/pic/516ed7cf56ba7d01eb09f524/"}
Does anybody know how could I get the file from GridFS via http request?
Many thanks!
You'll need to write your own view, but you can make it seem like it's part of the API. First, the view:
def api_image(pk):
obj = get_object_or_404(Model, pk=pk)
image_file = obj.file
return Response(image_file.read(),
mime_type='image/png') # or whatever the MIME type is
Then, you can map it in your urls.py:
url('^/api/v1/pic/(?P<pk>\w+)/file/$', api_image)
And to make sure tastypie shows what you want in the output:
def dehydrate_file(self, bundle):
return '/api/v1/pic/%s/file/' % (bundle.obj.id)
Just make sure the fake API view appears ahead of your actual API definitions, and you should be all set!
Paul's hint was very useful. Here i have implemented this completely in tastypie manner for uploading and downloading images.
Here you go..
1. Overriding deseriazer to support 'multipart'.
class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultipartResource, self).deserialize(request, data, format)
2. Model class
class Research(Document):
user = ReferenceField(User)
academic_year = StringField(max_length=20)
subject = StringField(max_length=150)
topic = StringField(max_length=50)
pub_date = DateTimeField()
authored = StringField(max_length=20)
research_level = StringField(max_length=20)
paper_presented = BooleanField()
thesis_written = BooleanField()
proof_image = ImageField()
3. Resource class
class ResearchResource(MultipartResource, MongoEngineResource):
class Meta:
queryset = Research.objects.all()
list_allowed_methods = ['get','post']
resource_name = 'research'
authentication = SessionAuthentication()
authorization = Authorization()
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name,
self.wrap_view('dispatch_list'), name="api_dispatch_list"),
#url to download image file.
url(r"^(?P<resource_name>%s)/(?P<pk>\w+)/file/$"% self._meta.resource_name,
self.wrap_view('get_image'), name="api_get_image"),
]
#Preparing image url dynamically
def dehydrate_proof_image(self, bundle):
return '/api/v1/%s/%s/file/' % (self._meta.resource_name,bundle.obj.id)
#view will call based on image url to download image.
def get_image(self, request, **kwargs):
obj = Research.objects.get(id=kwargs['pk'])
image_file = obj.proof_image
return HttpResponse(image_file.read(), content_type="image/jpeg"))
Hope this will be very useful for everyone in future. :)