Web API 2 POST request simulation in POSTMAN Rest Client
71,321
Well, make sure that you specify raw
and set the Content-Type
request header to application/json
. And then go ahead and specify the body of the POST request that will match your view model structure:
{
"id": 1,
"key": "some key",
"name": "some name of course",
"password": "the hyper secret",
"teamId": 256,
"stat": {
"playerId": 115,
"firstName": "John",
"lastName": "Smith",
"title": "His Royal Majesty",
"emailAddress": "[email protected]",
"phoneNumbers": [
{ "value": "123", "extension": "05" },
{ "value": "456", "extension": "45" }
],
"teamId": 678
}
}
So your actual payload's gonna look like that at protocol level:
POST /NFL/Players HTTP/1.1
Host: localhost:9888
Content-Type: application/json
Content-Length: 582
{
"id": 1,
"key": "some key",
"name": "some name of course",
"password": "the hyper secret",
"teamId": 256,
"stat": {
"playerId": 115,
"firstName": "John",
"lastName": "Smith",
"title": "His Royal Majesty",
"emailAddress": "[email protected]",
"phoneNumbers": [
{ "value": "123", "extension": "05" },
{ "value": "456", "extension": "45" }
],
"teamId": 678
}
}
Author by
GilliVilla
Updated on October 28, 2020Comments
-
GilliVilla over 3 years
I am using ASP.NET Web API 2 with attribute routing.
I have the following
PlayerModel
.public class PlayerModel { public int Id { get; set; } public string Key { get; set; } public string Name { get; set; } public string Password { get; set; } public int TeamId { get; set; } public PlayerStatModel Stat{ get; set; } } public class PlayerStatModel { public int PlayerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Title { get; set; } public string EmailAddress { get; set; } public IEnumerable<PhoneNumberModel> PhoneNumbers { get; set; } public int TeamId { get; set; } } public class PhoneNumberModel { public string Value { get; set; } public string Extension { get; set; } }
Which in turn is passed into
PostPlayer
for player creation.[HttpPost("", RouteName = "PostPlayer")] public PlayerModel PostPlayer(PlayerModel player) { var playerObject = this.GetObject(player)); this._manager.CreatePlayer(playerObject ); return this.GetPlayer(playerObject.Id); }
My integration tests pass and I am able to verify that player is indeed created when
CreatePlayer
is invoked.How can I model this
POST
request in the POSTMAN Rest Client in Google Chrome? -
NomanJaved over 7 yearscan you explain it further i am facing little problem.
-
Prageeth godage over 6 yearsIn ASP.net core add [FromBody] tag front of the parameter.
-
Ikram Shah over 5 yearsHere is the link which explains why we should use [FromBody] .net APIs stackoverflow.com/questions/24625303/…