class DemoAPIView(APIView):
parser_classes = (JSONParser, FormParser, MultiPartParser)
def post(self, request, format=None):
data = request.data
attached_file = request.FILES['attached_file']
description_image = data.get('descriptionImage', None)
attachment = dict(object_id=174, project=1, attached_file=attached_file,
description=description_image)
header = {'Authorization': 'Bearer eyJ1c2VyX2F1dGhlbnRpY2F0aW9uX2lkIjoxNn0:1gSTf6:KbOb0yhqC-qVPTEPRoiVBBZjN6M'}
r = requests.post('demo/api/v1/issues/attachments', data=attachment, headers=header)
print(r.json())
return Response({'error': 'ERROR XD'}, status=status.HTTP_400_BAD_REQUEST)
api引导我的错误如下:
^{pr2}$这是我从客户那里发来的图像。 把附件打印出来,给我看看网址,显然这就是问题所在。 有没有办法得到完整的文件?在
表单客户端:
const bodyFormData = new FormData();
if (typeof this.image !== 'string') {
bodyFormData.append('attached_file', this.image);
bodyFormData.append('descriptionImage', this.descriptionImage);
}
this.axios.post('/api/', bodyFormData,
{ headers: { 'Content-Type': 'multipart/form-data' } })
.then((response) => {
this.isSending = false;
this.$snackbar.open(response.data.results);
this.feedback = {};
this.image = {};
}).catch((err) => {
this.$snackbar.open({ message: err.response.data.error, type: 'is-danger' });
this.isSending = false;
});
提交的表单可能不是
enctype="multipart/form-data
。在如Django docs所述:
对于DRF,您可能需要FileUploadParser(请参见this)
相关问题 更多 >
编程相关推荐