1

I'm spending hours to solve this problem. The problem is I can't send POST's body data. I'm getting 500 error from the server.

Here is my HTTP request on React Native (I'm thinking I may have wrong axios request). There might be problem on http body?

export const createCloth = (token, hType, clothObject) => async dispatch => {
  let headers = { 'Authorization': `JWT ${token}`};
  if(hType==1) {
    headers = { 'Authorization': `JWT ${token}`};
  } else if (hType==2) {
    headers = { 'Authorization': `Bearer ${token}`};
  }

  let {image, text, clothType, ... , onlyMe} = clothObject;

  console.log(text); <- printing ok
  console.log(bigType); <- printing ok

  let response = await axios.post(`${ROOT_URL}/clothes/create/`, {

    text, clothType, ..., onlyMe
  }, {headers});

  console.log(response.data); <<< 500 HTTP error

Here is my part of Backend API.

class ClothCreateAPIView(APIView):
    def post(self, request, format=None):
        # Create Cloth
        # self.request.POST.get('user_id')
        print('--------REQUEST-------')
        print(request)


        logged_in_user = self.request.user
        content = self.request.POST.get('text')
        cloth_type = self.request.POST.get('clothType')
         only_me = self.request.POST.get('onlyMe')
        ...
        print('----------PRINTING CLOTH CONTENT')
        print(logged_in_user) <- printing my username (GOOD)
        print(cloth_type) <- printing None always (BAD)
        print(content) <- printing None always (BAD)
        print(self.request.POST) <- prints <QueryDict: {}>

At the last two line, why are they always printing None? I'm checking these syntax back and forth more than twenty times. This is so frustrating

Nothing wrong with self.request.POST.get('somthing') syntax but the reason why it's not working is we have problem on the syntax of axios request

0

2 Answers 2

16

By default, axios serializes the request data to json. You can use json.loads to deserialize it.

import json
data = json.loads(request.body.decode('utf-8'))

Alternatively, if you want to use request.POST, see the [axios docs] for options to send data in the application/x-www-form-urlencoded format.

Sign up to request clarification or add additional context in comments.

4 Comments

WOW wonderful. Could I ask one more question? Can application/x-www-form-urlencoded format harm the request any way? I'm sending an image and not familiar with application/x-www-form-urlencoded
or I deserialize the image and it won't matter?
For non-ajax requests, you use multipart/form-data instead of application/x-www-form-urlencoded for uploads. I'm not sure what the best way of posting an image with an ajax request would be.
For that matter, I just started a bouty question here if you are interested in: stackoverflow.com/questions/46951617/…
3

If you are using a djangorestframework to build your views you should be accessing the data through request.data rather than request.POST. DRF will automatically parse the json for you and give you access to a dictionary like you are expecting request.POST to have.

This also works for other http methods unlike request.POST.

http://www.django-rest-framework.org/api-guide/requests/

1 Comment

you are the life server :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.