如何在Postman中传递复杂对象参数?

huangapple go评论78阅读模式
英文:

how to pass complex object parameters in postman?

问题

我的C# API接受两个参数,一个是复杂对象,另一个是字符串:

[HttpPost, Route("paramtester")]
public string paramtester(people user, string message) {
    return user.name + message;
}

如何通过请求体传递JSON对象?我尝试传递一个序列化的JSON对象,但完全不起作用:

{
    "user": {
        "name": "John Doe",
        "age": 30,
        "country": "USA"
    },
    "message": "Hello, World!"
}

这将返回:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-40ce97917410bdcff73bb4d8737ed944-2c7f4bf20348d6b7-00",
    "errors": {
        "name": [
            "The name field is required."
        ],
        "address": [
            "The address field is required."
        ],
        "message": [
            "The message field is required."
        ]
    }
}
英文:

my c# api takes two parameters, one complex object and one string:

[HttpPost, Route("paramtester")]
public string paramtester(people user, string message) {
    return user.name + message;}

how do I pass by a json object in the body? I have tried passing in a serial json object which doesn't work at all:

{
    "user": {
        "name": "John Doe",
        "age": 30,
        "country": "USA"
    },
    "message": "Hello, World!"
}

this will return:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-40ce97917410bdcff73bb4d8737ed944-2c7f4bf20348d6b7-00",
    "errors": {
        "name": [
            "The name field is required."
        ],
        "address": [
            "The address field is required."
        ],
        "message": [
            "The message field is required."
        ]
    }
}

答案1

得分: 1

有两种正确的方法:

  1. 创建一个包含复杂对象 user 和简单字符串 message 的请求对象

    public class ParamTesterRequest
    {
       public People User {get; set;}
       public string Message {get; set;}
    }
    

    因此,您的Web API 变成了

     [HttpPost("paramtester")]
     public string ParamTester(ParamTesterRequest request)
     {
        return request.User.Name + request.Message;
     }
    
  2. 另一种方法是如Selvin所说,将复杂参数 user 从请求体中获取,将另一个作为查询参数。因此,您的Web API 变成了

     [HttpPost("paramtester")]
     public string ParamTester([FromQuery] string message, [FromBody] People user)
     {
        return user.Name + message;
     }
    

注意:第一种答案是最简单和最清晰的。

英文:

There is 2 correct approaches:

  1. create a request object that contains a complex object user and a silple string message

    public class ParamTesterRequest
    {
       public People User {get; set;}
       public string Message {get; set;}
    }
    

    So your web API become

     [HttpPost("paramtester")]
     public string ParamTester(ParamTesterRequest request)
     {
        return request.User.Name + request.Message;
     }
    
  2. the other one is as said by Selvin, use the complex parameter user from body and the other one as query parameter. So your web API become

     [HttpPost("paramtester")]
     public string ParamTester([FromQuery] string message, [FromBody] People user)
     {
        return user.Name + message;
     }
    

NOTE: the first answer is that it is the simplest and cleanest

答案2

得分: 0

你应该创建一个包括以下内容的DTO(数据传输对象):

public sealed class MyDto
{
    [JsonPropertyName("user")]
    public User user { get; set; }

    [JsonPropertyName("message")]
    public string message { get; set; }
}

public sealed class User
{
    [JsonPropertyName("name")]
    public string name { get; set; }

    [JsonPropertyName("age")]
    public int age { get; set; }

    [JsonPropertyName("country")]
    public string country { get; set; }
}

然后在端点上添加 [FromBody]

[HttpPost, Route("paramtester")]
public string paramtester([FromBody] MyDto myDto) {
    return myDto.Name + myDto.Message;
}
英文:

You should create a DTO (Data Transfer Object) that includes both:

    public sealed class MyDto
    {
        [JsonPropertyName("user")]
        public User user { get; set; }

        [JsonPropertyName("message")]
        public string message { get; set; }
    }

    public sealed class User
    {
        [JsonPropertyName("name")]
        public string name { get; set; }

        [JsonPropertyName("age")]
        public int age { get; set; }

        [JsonPropertyName("country")]
        public string country { get; set; }
    }

Then add [FromBody] to the endpoint:

[HttpPost, Route("paramtester")]
public string paramtester([FromBody] MyDto myDto) {
    return myDto.Name + myDto.Message;}

huangapple
  • 本文由 发表于 2023年7月20日 22:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定