185👍
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:
- After setting request method to POST, click to the ‘body’ tab.
- 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.
- Then select ‘raw’ and paste your JSON file. Also, just next to the binary choice, you’ll see ‘Text’ is clicked. Make it JSON.
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()
37👍
Now you can hover the key input and select “file”, which will give you a file selector in the value column:
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-Error: No module named psycopg2.extensions
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
17👍
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.
- [Django]-How to mix queryset results?
- [Django]-Django-social-auth django-registration and django-profiles — together
- [Django]-Why does django's prefetch_related() only work with all() and not filter()?
14👍
It can be done in three ways
1. Go to Body > Form-data > Select Files in the column
2. Go to Body > binary > Select File
3. Encode image to base64 string and pass it through postman Body > raw > JSON like mentioned in the attached screenshots
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.
- [Django]-Iterate over model instance field names and values in template
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Fastest way to get the first object from a queryset in django?
- [Django]-Django: import auth user to the model
- [Django]-Where does pip install its packages?
2👍
Follow the below steps:
- No need to give any type of header.
-
Select body > form-data and do same as shown in the image.
-
Now in your Django view.py
def post(self, request, *args, **kwargs): image = request.FILES["image"] data = json.loads(request.data['data']) ... return Response(...)
- You can access all the keys (id, uid etc..) from the data variable.
- [Django]-Django Model MultipleChoice
- [Django]-How do I use CSS in Django?
- [Django]-How to rename items in values() in Django?
1👍
- [Django]-Passing variable urlname to url tag in django template
- [Django]-UUID as default value in Django model
- [Django]-Delete multiple objects in django