'POST to asp.net web api fails from nodejs but works from postman

Please I have been battling with this issue. I have some api controllers and they work well with postman but posting from nodejs using axios I have errors. Here's my axios code:

 var postData = JSON.stringify(req.body);
      var postdataToString = JSON.parse(postData);
      postdataToString['grant_type'] = "password";
      delete postdataToString['submit'];
      var PostBody = JSON.stringify(postdataToString);

and

let response =  axios({
      method: 'POST',
      
      url: url, 
      proxy: undefined,
      data: PostBody, 
      headers:{'Content-Type': 'application/x-www-form-urlencoded'},
      headers:{'Accept': 'application/json'}
  }) 

The error output from the console shows the json data is correct.

Here is a piece from the api code:

public async Task<IHttpActionResult> Register(UserModel userModel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
       

        IdentityResult result = await _repo.RegisterUser(userModel);

the error is invalid request.

Thanks for your help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source