C# POSTMAN Rest Client 中的 Web API 2 POST 请求模拟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19037636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:57:31  来源:igfitidea点击:

Web API 2 POST request simulation in POSTMAN Rest Client

c#httprestpostasp.net-web-api

提问by GilliVilla

I am using ASP.NET Web API 2 with attribute routing.

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

I have the following PlayerModel.

我有以下内容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 PostPlayerfor player creation.

依次传递PostPlayer给玩家创建。

[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 CreatePlayeris invoked.

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

How can I model this POSTrequest in the POSTMAN Rest Client in Google Chrome?

如何POST在 Google Chrome 的 POSTMAN Rest Client 中对此请求建模?

enter image description here

在此处输入图片说明

采纳答案by Darin Dimitrov

Well, make sure that you specify rawand set the Content-Typerequest header to application/json. And then go ahead and specify the body of the POST request that will match your view model structure:

好吧,请确保您指定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": "[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
    }
}