Laziale Laziale - 2 years ago 213
Ajax Question

postman params not sent to web api controller

I have this web api controller:

public class LoginController : ApiController
private mw2devnew15Entities db = new mw2devnew15Entities();

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:

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) {

But when I'm trying to use Postman to place POST call, the JObject is null.

enter image description here

Any idea why?

Answer Source

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:

Add a content type header with the value of application/json:

enter image description here

and for your request body select raw and then add your JSON:

enter image description here

this will send the following request same as your JavaScript code:

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" }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download