网页API POST方法返回HTTP / 1.1 500内部服务器错误 [英] Web API POST method returns HTTP/1.1 500 Internal Server Error

查看:7220
本文介绍了网页API POST方法返回HTTP / 1.1 500内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说我已经使用Web API的POST方法时,得到了500内部服务器错误。 GET方法是工作的罚款,刚开错误POST。

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.

我使用菲德勒发送POST请求:

I am using fidler to send post request :

响应头:
HTTP / 1.1 500内部服务器错误

Response Header: HTTP/1.1 500 Internal Server Error

请求头:
用户代理:提琴手
主机:本地主机:45379
内容类型:应用程序/ jsonContent长度:41
内容长度:41

Request Header: User-Agent: Fiddler Host: localhost:45379 Content-Type: application/jsonContent-Length: 41 Content-Length: 41

请求正文:
{ID用户=123456789,用户名=橙色}

Request Body: {"iduser"="123456789","username"="orange"}

这是我的code为POST方法:

     // 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什么可能可能出了错?为什么它不是让我的帖子?

推荐答案

在您的文章中的数据不是有效的JSON对象,这是什么模型绑定期待。(内容类型:应用程序/ JSON)

The data in your post is not a valid JSON-object, which is what the model binder is expecting (Content-Type: application/json).

{"iduser"="123456789","username"="orange"}

试着用替换您=:看看你的身体情况如何。您code工作在我的机器上使用请求的改变。

Try replacing your = with : and see how you get on. Your code works on my machine with those alterations in the request.

POST http://localhost:20377/api/test/Postuser HTTP/1.1
Host: localhost:20377
Connection: keep-alive
Content-Length: 42
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Origin: chrome-extension://fhjcajmcbmldlhcimfajhfbgofnpcjmb
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,nb;q=0.4,de;q=0.2

{"iduser":"123456789","username":"orange"}

这篇关于网页API POST方法返回HTTP / 1.1 500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆