I have this web api controller:
我有这个web api控制器:
public class LoginController : ApiController
{
private mw2devnew15Entities db = new mw2devnew15Entities();
[System.Web.Http.HttpGet]
public string Post()
{
string authenticationToken = "";
return authenticationToken;
}
[System.Web.Http.AcceptVerbs("GET", "POST")]
public HttpResponseMessage Post(JObject data)
{
dynamic json = data;
LoginForm loginF = new LoginForm();
loginF.username = json.username;
loginF.password = json.password;
return Request.CreateResponse(HttpStatusCode.OK);
}
}
I'm able to post correctly with this ajax call:
我能用这个ajax调用正确发布:
jQuery.ajax({
type: "POST",
url: "http://localhost:5832/api/Login",
data: JSON.stringify({ username: 'joep11aul1234', password: '1212213' }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
But when I'm trying to use Postman to place POST call, the JObject is null.
但是当我尝试使用Postman进行POST调用时,JObject为null。
Any idea why?
知道为什么吗?
1 个解决方案
#1
2
Using Postman you're not reproducing the same request as your JavaScript code since you posting the parameters in the query string. What you shoud do instead is something like this:
使用Postman,您不会再生成与JavaScript代码相同的请求,因为您在查询字符串中发布了参数。你要做的事情是这样的:
Add a content type header with the value of application/json
:
使用application / json的值添加内容类型标头:
and for your request body select raw
and then add your JSON:
并为您的请求正文选择raw然后添加您的JSON:
this will send the following request same as your JavaScript code:
这将发送与JavaScript代码相同的以下请求:
POST /api/Login HTTP/1.1
Host: localhost:5832
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4bf25ded-7548-77f9-3389-fa16a5d50087
{ "username": "joep11aul1234", "password": "1212213" }
#1
2
Using Postman you're not reproducing the same request as your JavaScript code since you posting the parameters in the query string. What you shoud do instead is something like this:
使用Postman,您不会再生成与JavaScript代码相同的请求,因为您在查询字符串中发布了参数。你要做的事情是这样的:
Add a content type header with the value of application/json
:
使用application / json的值添加内容类型标头:
and for your request body select raw
and then add your JSON:
并为您的请求正文选择raw然后添加您的JSON:
this will send the following request same as your JavaScript code:
这将发送与JavaScript代码相同的以下请求:
POST /api/Login HTTP/1.1
Host: localhost:5832
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4bf25ded-7548-77f9-3389-fa16a5d50087
{ "username": "joep11aul1234", "password": "1212213" }