I'm uploading a file and along with it I've few data fields to parse
json in restapi
views.py
class FileView(APIView):
def get(self,request):
#request.content_type= 'multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL'
content = r'Submit File.'
return Response(content, status=status.HTTP_200_OK)
def post(self,request,*args,**kwargs):
try:
print(request.content_type)
print(request.data)
parser_classes =(MultiPartParser, JSONParser)
if not request.data.get('filename'):
content = "File is not uploaded !! Please upload a sample"
return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
file = request.data.get('filename')
print(file)
request_Details = { }
request_Details['creator'] = request.data.get("creator", None)
request_Details['enable'] = request.data.get('enable', None)
request_Details['type'] = request.data.get('type', None)
request_Details['filename'] = request.data.get('filename')
requestsData = requests(filename=request_Details['filename'],creator=request_Details['creator'],enable=request_Details['enable'],type=request_Details['type'],)
requestsData.save()
content = "File submitted successfully"
return Response(content, status=status.HTTP_200_OK)
except Exception as exp:
logger.error("Exception in submitRequestView POST function ", exc_info=True)
content = {'Exception': 'Internal Error'}
return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
model.py
class requests(models.Model):
id = models.AutoField(primary_key=True)
creator = models.CharField(max_length=255)
enable = models.CharField(max_length=255)
type = models.CharField(max_length=255)
filename = models.FileField(max_length=1000, null=True)
def __str__(self):
return self.id
serializer.py
class RequestsSerializer(serializers.ModelSerializer):
class Meta:
model = models.requests
fields = ('filename', 'creator', 'type', 'enable',)
urls.py
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^File_upload/', views.FileView.as_view()),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
setting.py(I've attached the media path code only)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'#
My json request is as follows
{
"filename":"C://Users//Administrator//Desktop//file1.zip",
"creator":"aarav",
"enable":"yes",
"type":"dll"
}
and making use of media type as multipart/form-data not application/json , and MultipartParser and JsonParser classes in views.py.
I've tested my code from postman and I'm able to upload image to media folder and store the filename and other details in database.
Now when I try to POST from django restapi as shown in image attached , and I get the below error
Module : File_upload ERROR Exception in submitRequestView POST function
Traceback (most recent call last):
File "C:UsersxxxDesktopfileupload
estapi_fileviews.py", line 464, in post
print(request.data)
File "C:Python27libsite-packages
est_framework
equest.py", line 212, in data
self._load_data_and_files()
File "C:Python27libsite-packages
est_framework
equest.py", line 275, in _load_data_and_files
self._data, self._files = self._parse()
File "C:Python27libsite-packages
est_framework
equest.py", line 350, in _parse
parsed = parser.parse(stream, media_type, self.parser_context)
File "C:Python27libsite-packages
est_frameworkparsers.py", line 116, in parse
raise ParseError('Multipart form parse error - %s' % six.text_type(exc))
ParseError: Multipart form parse error - Invalid boundary in multipart: None
How to solve this and post request using restframework and not use any templates..??