最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't get POST data from Django (sending it from React) - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question edited Nov 2, 2017 at 9:04 merry-go-round asked Nov 2, 2017 at 8:54 merry-go-roundmerry-go-round 4,62513 gold badges57 silver badges111 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 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.

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/api-guide/requests/

发布评论

评论列表(0)

  1. 暂无评论