英文:
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
有两种正确的方法:
-
创建一个包含复杂对象
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; }
-
另一种方法是如Selvin所说,将复杂参数
user
从请求体中获取,将另一个作为查询参数。因此,您的Web API 变成了[HttpPost("paramtester")] public string ParamTester([FromQuery] string message, [FromBody] People user) { return user.Name + message; }
注意:第一种答案是最简单和最清晰的。
英文:
There is 2 correct approaches:
-
create a request object that contains a complex object
user
and a silple stringmessage
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; }
-
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;}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论