You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When testing my API I have encountered an issue. Consider this code, note that the url is set previously
comment_data= {'content':'This is a comment'}
response=self.client.post(url, comment_data)
Sent to this view
@decorators.action(detail=True, methods=['post'], url_path="post_answer/?", permission_classes=[permissions.IsAuthenticated])defpost_answer(self, request:Request, pk=None):
data=dict(request.data)
# add user and comment parent datadata['relatedQAndA'] =pkdata['creator'] =request.userserializer=CommentSetSerializer(data=data)
# try to serialize and return 400 in case of errorifnotserializer.is_valid(raise_exception=False):
returnResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.save()
# return a nice formatserializer=CommentGetSerializer(Comment.objects.get(pk=serializer.data['id']))
returnResponse(serializer.data, status=status.HTTP_201_CREATED)
This responds with a 400 error: Not a valid string.
When investigating i found that the View does not receive the correct data dict! reponse.data={'content':['This is a comment']} The string has been packaged into a list for some reason.
The current solution I have found is to add format=json parameter to the post.
Any idea why this is happening and is this normal behavior?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
When testing my API I have encountered an issue. Consider this code, note that the url is set previously
Sent to this view
This responds with a 400 error:
Not a valid string.
When investigating i found that the View does not receive the correct data dict!
reponse.data={'content':['This is a comment']}
The string has been packaged into a list for some reason.The current solution I have found is to add
format=json
parameter to the post.Any idea why this is happening and is this normal behavior?
Beta Was this translation helpful? Give feedback.
All reactions