Reputation: 179
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
Upvotes: 0
Views: 58
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