Angular - Troubleshooting - FirstRun - 400

400 Bad Request

https://wiki-tech.net/en/errors/b/0x/http%20errors%20400/?msclkid=5650711403441d76cfa92cbbcf709159&utm_source=bing&utm_medium=cpc&utm_campaign=WT.S.USA.Errors.P&utm_term=http%20error%20400&utm_content=Error

400 could mean the packet you are sending to the server is malformed. For example:

  • a null in a Boolean field
  • an int field receiving “0” from the program calling the api. (9/12/2020 - I had in input control with a type of “textbox” - the number went into the model as a string, when I changed the control type to “number” it functioned correctly)

I found, in chrome if I F12, and then switched to the network tab, and replicated the error I would see a row showing the runtime error. The Response tab contains a list of errors associated with the packet and one of them was escalateTo was expecting a numeric value.

The pattern is that if you are passing invalid data type. (for example a required date, or Boolean and you pass a null) then you will likely get the 400 error.

For me, I attempt to duplicate this in an API tester (like PostMan, or the API Tester). Because if you have one error, you’ll likely have more than one error. Using the API tester at this point makes sense because once you nail down the correct packet, you can apply the lessons learned to your code.

This will also fail if you have logic like this

1
2
3
4
5
if (!ModelState.IsValid)
return BadRequest(ModelState);

var result = _programsService.AddProgram(programsViewModel, GetIpAddress());
return ReturnResult(result);

if the model being passed in is invalid (perhaps a validator failes), then the BadRequest is a 400