13

Assume I have some data as below,

{
    "name":"John",
    "age":30,
    "cars": 
    {
        "car_img_1":"car_img_file1",
        "car_img_2":"car_img_file2",
        "car_img_3":"car_img_file3"
    }
 }

How can I send it using POSTMAN with form-data?

NOTES
1. car_img_fileX will be the file(.jpg,.png etc types)
2. What I'd tried -->> POSTMAN Screenshot.
3. Local server builted with Django framework


Current Output
Receiving 5 different items/data instaed of Nested data--> see this Pycharm Debugger Output

JPG
  • 82,442
  • 19
  • 127
  • 206

3 Answers3

8

Try this:

cars[0][car_img_1]:car_img_file1
cars[1][car_img_2]:car_img_file2

You can insert it in "bulk-edit" mode.

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
  • In my request data the whole key value is a string( 'cars[0][car_img_1]': ['car_img_file1']), so serializer is not validating the data. Can you please tell me what to do? – Sam Jun 13 '19 at 07:18
1

I found this answer from this problem. Edited as per your code.

Convert your Image Fields to base64Image and send it through the JSON data.

All you need to do is:

  1. go to https://www.base64-image.de/ and convert the image to base64 format. Copy the encoded result.
  2. Install django-extra-fields package in your project from here
  3. In your serializer_class, import and change the image field to Base64ImageField:

serializers.py

...
from drf_extra_fields.fields import Base64ImageField
...
 
  1. Now, go to your postman and send the JSON data like the following. Remember to send that encoded image in your image field in JSON.
{
    "name":"John",
    "age":30,
    "cars": 
    {
        "car_img_1":"<base64 encoded image>",
        "car_img_2":"<base64 encoded image>",
        "car_img_3":"<base64 encoded image>"
    }
 }
AlexPy
  • 94
  • 7
0

You should try in this way (Hint: Watch carefully form-data key names):

enter image description here

  • So, the key point is the indexing syntax for each KVP/data element? - Can you please make that clear, and copy the text of the body into the Answer. – ryanwebjackson Jun 26 '23 at 15:08