As the title says i have got a 500 internal server error when using post method of a Web API. The Get method is working fine, just getting error in POST.
I am using fidler to send post request :
Response Header: HTTP/1.1 500 Internal Server Error
Request Header: User-Agent: Fiddler Host: localhost:45379 Content-Type: application/jsonContent-Length: 41 Content-Length: 41
Request Body: {"iduser"="123456789","username"="orange"}
Here is my code for post method:
// POST api/User
public HttpResponseMessage Postuser(user user)
{
if (ModelState.IsValid)
{
db.users.Add(user);
db.SaveChanges();
HttpResponseMessage response =R equest.CreateResponse(HttpStatusCode.Created, user);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.iduser }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Sooooooo what could have possibly gone wrong? Why its not allowing me to POST?
The data in your post is not a valid JSON-object, which is what the model binder is expecting (Content-Type: application/json).
Try replacing your = with : and see how you get on. Your code works on my machine with those alterations in the request.