I have a very simple .NET Web API hosted in Azure, with two very simple methods:
[EnableCors(origins: "http://simpleapiearl.azurewebsites.net", headers: "*", methods: "*")]
public class EnvelopesController : ApiController {
// GET: api/Envelopes
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
// POST: api/Envelopes
public string Post([FromBody]Envelope env) {
return "rval: " + env + " (and my addition to env)";
}
}
I have created a simple plunk to call these methods. In my AngularJS code, I'm making two very simple $http calls. The GET works fine. The POST, however, always returns a "400 (Bad Request)", followed shortly in my WebStorm console by "XMLHttpRequestion cannot load ... Invalid HTTP status code 400".
Any and all suggestions appreciated!
EDIT!!
Hello, and thanks for the very fast responses. I just realized I forgot to add some very relevant detail.
The parameter to my POST method is what I call an Envelope object, which contains two properties: listingId (int)
, and Description (string)
. When I add the urlencoded Content-Type header, and pass '{"listingId":1234, "Description":"some description"}'
as my POST data, the env parameter in my POST method on the server is NULL (that is, it seems unable to parse the JSON I'm providing). Sorry this was omitted from original post.
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).
In rare cases, the cause for a 400 Bad Request error is due to issues with the server and not the client. To check if there's an issue with the server, try loading the site from a different browser and device. If you can't access the site from a different browser and device, then it's likely a server-side problem.
The 400 Bad Request Error is an HTTP response status code indicating that the server was unable to process the request sent by the client due to invalid syntax.
A 400 Bad Request, also known as a 400 error or HTTP error 400, is perceived by the server as a generic client error and it is returned when the server determines the error doesn't fall in any of the other status code categories.
I think this post would be helpful.
How do I POST urlencoded form data with $http in AngularJS?
By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json". When we want to post the value as a FORM post, we need to change the serialization algorithm and post the data with the content-type, "application/x-www-form-urlencoded".
This is the modified plnkr from yours. Your code is missing conversion of JSON to encoded url.
http://plnkr.co/edit/4aeEDz73qgHSfOv1sBgn?p=preview
$http({
method: 'POST',
url: 'http://simpleApiEarl.azurewebsites.net/api/envelopes',
data: env,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
}).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With