【问题标题】:Web API 2 POST request simulation in POSTMAN Rest ClientPOSTMAN Rest Client 中的 Web API 2 POST 请求模拟
【发布时间】:2013-10-02 23:25:51
【问题描述】:

我正在使用带有属性路由的 ASP.NET Web API 2。

我有以下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; }
}

然后将其传递给PostPlayer 用于创建玩家。

[HttpPost("", RouteName = "PostPlayer")]
public PlayerModel PostPlayer(PlayerModel player)
{
    var playerObject = this.GetObject(player));
    this._manager.CreatePlayer(playerObject );

    return this.GetPlayer(playerObject.Id);
}

我的集成测试通过了,我能够验证在调用CreatePlayer 时确实创建了播放器。

如何在 Google Chrome 的 POSTMAN Rest Client 中为这个 POST 请求建模?

【问题讨论】:

    标签: c# http rest post asp.net-web-api


    【解决方案1】:

    好吧,确保您指定raw 并将Content-Type 请求标头设置为application/json。然后继续并指定与您的视图模型结构匹配的 POST 请求的主体:

    {
        "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
        }
    }
    

    所以你的实际有效负载在协议级别看起来像这样:

    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
        }
    }
    

    【讨论】:

    • 你能进一步解释一下吗?我遇到了小问题。
    • 在 ASP.net core 中在参数前面添加 [FromBody] 标签。
    • 这里的链接解释了为什么我们应该使用 [FromBody] .net APIs stackoverflow.com/questions/24625303/…
    猜你喜欢
    • 2017-10-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2014-11-19
    • 2013-02-25
    • 2018-07-14
    • 2018-08-04
    相关资源
    最近更新 更多