[英]Web API 2 POST request simulation in POSTMAN Rest Client
我正在使用帶有屬性路由的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
請求建模?
好吧,請確保指定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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.