网页API 2 POST请求模拟邮差REST客户端 [英] Web API 2 POST request simulation in POSTMAN Rest Client

查看:172
本文介绍了网页API 2 POST请求模拟邮差REST客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ASP.NET Web API 2属性的路由。

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

我有如下的 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 确实创建。

我如何可以模拟在谷歌浏览器邮差REST客户端本 POST 的要求吗?

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

推荐答案

好了,请确保您指定原材料并设置的Content-Type 请求头应用程序/ JSON 。然后继续前进,指定POST请求,将符合您的视图模型结构的主体:

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

这篇关于网页API 2 POST请求模拟邮差REST客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆