Web API 2 POST request simulation in POSTMAN Rest

2019-02-01 05:44发布

问题:

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?

回答1:

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": "john.smith@buckingampalace.com",
        "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": "john.smith@buckingampalace.com",
        "phoneNumbers": [
            { "value": "123", "extension": "05" },
            { "value": "456", "extension": "45" }
        ],
        "teamId": 678
    }
}