"Post Image data using POSTMAN"

DjangoDjango ModelsDjango Rest-FrameworkPostman

Django Problem Overview


I am trying to POST data to my API. I have a model with an image field where:

image = models.ImageField()

I have an image on my local box, which I am trying to send. Am I sending it correctly?

{
   "id": "3", 
   "uid":"273a0d69",
   "uuid": "90",
   "image": "@/home/user/Downloads/tt.jpeg"
}

Django Solutions


Solution 1 - Django

That's not how you send file on postman. What you did is sending a string which is the path of your image, nothing more.

What you should do is;

  1. After setting request method to POST, click to the 'body' tab.
  2. Select form-data. At first line, you'll see text boxes named key and value. Write 'image' to the key. You'll see value type which is set to 'text' as default. Make it File and upload your file.
  3. Then select 'raw' and paste your json file. Also just next to the binary choice, You'll see 'Text' is clicked. Make it JSON.

form-data section

raw section

You're ready to go.

In your Django view,

from rest_framework.views import APIView
from rest_framework.parsers import MultiPartParser
from rest_framework.decorators import parser_classes

@parser_classes((MultiPartParser, ))
class UploadFileAndJson(APIView):

    def post(self, request, format=None):
        thumbnail = request.FILES["file"]
        info = json.loads(request.data['info'])
        ...
        return HttpResponse()

Solution 2 - Django

Now you can hover the key input and select "file", which will give you a file selector in the value column:

enter image description here

Solution 3 - Django

The accepted answer works if you set the JSON as a key/value pair in the form-data panel (See the image hereunder)

enter image description here

Nevertheless, I am wondering if it is a very clean way to design an API. If it is mandatory for you to upload both image and JSON in a single call maybe it is ok but if you could separate the routes (one for image uploading, the other for JSON body with a proper content-type header), it seems better.

Solution 4 - Django

It works for me:

  1. Goto Body
  2. Form-data
  3. Chose "File" in the first cell enter image description hereproperties

Solution 5 - Django

It can be done in three ways

1. Go to Body > Form-data > Select Files in the column

enter image description here

2. Go to Body > binary > Select File

enter image description here

3. Encode image to base64 string and pass it through postman Body > raw > JSON like mentioned in the attached screenshots

enter image description here

enter image description here

Then on the server-side, you can decode it that way

import base64
decode = base64.b64decode(data)

from django.core.files.base import ContentFile
file = ContentFile(decode, name=name)

Note: You could encode the file to base64 through that link and send it in the curl.

https://base64.guru/converter/encode/file

Solution 6 - Django

Follow the below steps:

  1. No need to give any type of header.
  2. Select body > form-data and do same as shown in the image. 1
  1. Now in your Django view.py

> def post(self, request, *args, **kwargs): > image = request.FILES["image"] > data = json.loads(request.data['data']) > ... > return Response(...)

  1. You can access all the keys (id, uid etc..) from the data variable.

Solution 7 - Django

You can upload your image from the binary tab if the endpoint is expecting just an image (not a key-value pair). enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUser_TargaryenView Question on Stackoverflow
Solution 1 - DjangoCagatay BarinView Answer on Stackoverflow
Solution 2 - DjangotheisofView Answer on Stackoverflow
Solution 3 - DjangoMarAjaView Answer on Stackoverflow
Solution 4 - DjangosworView Answer on Stackoverflow
Solution 5 - DjangoUmar AsgharView Answer on Stackoverflow
Solution 6 - DjangoJatin GoyalView Answer on Stackoverflow
Solution 7 - DjangoKirill KudaevView Answer on Stackoverflow