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

merry-go-round
  • 4,533
  • 10
  • 54
  • 102

2 Answers2

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 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` – merry-go-round Nov 02 '17 at 09:11
  • or I deserialize the image and it won't matter? – merry-go-round Nov 02 '17 at 09:13
  • 1
    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. – Alasdair Nov 02 '17 at 09:33
  • For that matter, I just started a bouty question here if you are interested in: https://stackoverflow.com/questions/46951617/how-to-include-image-uri-in-http-request – merry-go-round Nov 02 '17 at 09:39
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/

shmeem
  • 81
  • 2