0

Am using angullar2 with django rest framework. I am trying to post a string from the angular client :

 postAuthCode(code: string) { 
   let headers = new Headers({ 'Content-Type': 'application/json' });
   let options = new RequestOptions({ headers: headers });

   var body ={ "code" : code};

   return this.http
     .post(this.authCodeUrl, {body},options)
     .toPromise()
     .then(res => {  
        console.log("the post response", res );
                 return res ;
     })
    .catch(this.handleError);
}

The view class in the backend :

 class AdwordAuthCode(APIView):
    def post(self, request, format=None):

    """
     :param request:
    :param format:
    :return: the credentials from  the recieved code
    """
    data = request.body('code')

    print('data', data)

    return Response(data)

When i test in the client i get

enter image description here

1 Answer 1

2

As the error says, you're treating request.body as a function while it's a string. In return you get a 500 error HTML page which is expected.

Replace request.body('code') by request.data['code'] and that should do.

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

Comments

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.