2013-09-26 29 views
27

Sto utilizzando l'API Web ASP.NET 2 con il routing degli attributi.Web API 2 Simulazione richiesta POST in POSTMAN Rest Client

Ho il seguente 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; } 
} 

Che a sua volta viene passato in PostPlayer per la creazione di giocatore.

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

    return this.GetPlayer(playerObject.Id); 
} 

miei test di integrazione passano e io sono in grado di verificare che il giocatore è infatti creato quando CreatePlayer viene richiamato.

Come è possibile modellare questa richiesta POST nel client di riposo POSTMAN in Google Chrome?

enter image description here

risposta

64

Beh, assicurarsi che si specificare raw e impostare l'intestazione Content-Type richiesta di application/json. E poi andare avanti e specificare il corpo della richiesta POST che corrispondono alla tua struttura di vista del modello:

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

Quindi andando del carico attuale sembra che, a livello di protocollo:

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

si può spiegare ulteriormente sto affrontando un piccolo problema. – NomanJaved

+1

Nel nucleo ASP.net aggiungere il tag [FromBody] davanti al parametro. – Prageeth

Problemi correlati