sila
sila

Reputation: 179

'str' object is not callable django angular2

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

Upvotes: 0

Views: 58

Answers (1)

Linovia
Linovia

Reputation: 20966

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.

Upvotes: 2

Related Questions