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"
}
Option 1: Direct File Upload , From this method you can select form-data and set the type to file. Then select an image file by clicking on the button shown in the value column. The content type is automatically detect by postman but if you want you can set it with a relevant MIME type.
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;
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()
Now you can hover the key input and select "file", which will give you a file selector in the value column:
The accepted answer works if you set the JSON as a key/value pair in the form-data
panel (See the image hereunder)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With