多个 AddParameter 调用只添加第一个参数,忽略其余参数
Multiple AddParameter calls only add the first parameter, ignoring the rest
相关项目信息:
- Driver方法是.Net Core 3.1中的一个XUnit项目,
- API 是 .Net Core 3.1 中的一个 API/MVC 项目。
- 两者在同一个解决方案中,
- 我 运行 有两个 VS 2019 实例,以便在这个特定时刻进行测试和调试。
- 测试的设计被设置为进行完整的功能测试,因为需要对我无法控制的情况进行诊断,并且团队中的其他开发人员在这些情况下同意这种方法。
我查看了 https://github.com/restsharp/RestSharp/wiki/Recommended-Usage 上的推荐使用文档,他们在其中添加了多个参数,我的代码在功能上是相同的。问题在于,只有添加的第一个参数才能到达 API 端点...也就是说,只有添加的第一个参数才能通过 Request.Form
对象的线路:
//driver method:
url = "MyURLHere";
parameters.Add(new KeyValuePair<string, string>("folderName", FolderName)); //is added
parameters.Add(new KeyValuePair<string, string>("configurationName", ConfigurationName)); //is not added
response = CallBox(url, Method.POST, parameters.ToArray(), true);
//...some time later...
private IRestResponse CallBox(string url, Method method, KeyValuePair<string, string>[] parameters = null, bool isUpload = false) {
parameters = parameters ?? new KeyValuePair<string, string>[] { };
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(method);
foreach (KeyValuePair<string, string> param in parameters) {
request.AddParameter(param.Key, param.Value, ParameterType.RequestBody);
}
request.AddHeader("content-type", "multipart/form-data; boundary=-----------------------------28947758029299");
IRestResponse response = client.Execute(request);
return response;
}
重要API代码信息:
public class BoxController : ControllerBase {
...
[HttpPost("MyURL")]
[ProducesResponseType(typeof(ActionResult), 201)]
[ProducesResponseType(typeof(ActionResult), 400)]
[ProducesResponseType(typeof(ActionResult), 500)]
public async Task<IActionResult> MyAPIEndPoint() {
...
//Checking Request.Form["ObjectKeysHere"]
这是怎么回事,我该如何解决这个问题? (谢谢)
What's happening here ...
ParameterType.RequestBody
只能添加一次,因为请求只有一个BODY。所有后续添加都将被忽略。
Request Body
If this parameter is set, its value will be sent as the body of the
request. Only one RequestBody
parameter is accepted - the first one.
注意:强调我的。
这证实了所提供的原始示例中所展示的问题。
... and how do I fix this issue?
在这种情况下使用 ParameterType.GetOrPost
private IRestResponse CallBox(string url, Method method, KeyValuePair<string, string>[] parameters = null, bool isUpload = false) {
parameters = parameters ?? new KeyValuePair<string, string>[] { };
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(method);
foreach (KeyValuePair<string, string> parameter in parameters) {
request.AddParameter(parameter.Key, parameter.Value, ParameterType.GetOrPost);
}
IRestResponse response = client.Execute(request);
return response;
}
Get or Post
This behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in
the form url?name1=value1&name2=value2
.
On a POST or PUT Requests, it depends on whether or not you have files
attached to a Request. If not, the Parameters will be sent as the body
of the request in the form name1=value1&name2=value2
. Also, the
request will be sent as application/x-www-form-urlencoded
.
In both cases, name and value will automatically be url-encoded.
If you have files, RestSharp will send a multipart/form-data
request. Your parameters will be part of this request in the form:
Content-Disposition: form-data; name="parameterName"
ParameterValue
注意:强调我的。
我认为你应该修改你的代码:
foreach (KeyValuePair<string, string> param in parameters)
{
Require.Argument(param.Key, param.Value);
request.AddParameter(new Parameter(param.Key, param.Value, ParameterType.RequestBody));
}
相关项目信息:
- Driver方法是.Net Core 3.1中的一个XUnit项目,
- API 是 .Net Core 3.1 中的一个 API/MVC 项目。
- 两者在同一个解决方案中,
- 我 运行 有两个 VS 2019 实例,以便在这个特定时刻进行测试和调试。
- 测试的设计被设置为进行完整的功能测试,因为需要对我无法控制的情况进行诊断,并且团队中的其他开发人员在这些情况下同意这种方法。
我查看了 https://github.com/restsharp/RestSharp/wiki/Recommended-Usage 上的推荐使用文档,他们在其中添加了多个参数,我的代码在功能上是相同的。问题在于,只有添加的第一个参数才能到达 API 端点...也就是说,只有添加的第一个参数才能通过 Request.Form
对象的线路:
//driver method:
url = "MyURLHere";
parameters.Add(new KeyValuePair<string, string>("folderName", FolderName)); //is added
parameters.Add(new KeyValuePair<string, string>("configurationName", ConfigurationName)); //is not added
response = CallBox(url, Method.POST, parameters.ToArray(), true);
//...some time later...
private IRestResponse CallBox(string url, Method method, KeyValuePair<string, string>[] parameters = null, bool isUpload = false) {
parameters = parameters ?? new KeyValuePair<string, string>[] { };
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(method);
foreach (KeyValuePair<string, string> param in parameters) {
request.AddParameter(param.Key, param.Value, ParameterType.RequestBody);
}
request.AddHeader("content-type", "multipart/form-data; boundary=-----------------------------28947758029299");
IRestResponse response = client.Execute(request);
return response;
}
重要API代码信息:
public class BoxController : ControllerBase {
...
[HttpPost("MyURL")]
[ProducesResponseType(typeof(ActionResult), 201)]
[ProducesResponseType(typeof(ActionResult), 400)]
[ProducesResponseType(typeof(ActionResult), 500)]
public async Task<IActionResult> MyAPIEndPoint() {
...
//Checking Request.Form["ObjectKeysHere"]
这是怎么回事,我该如何解决这个问题? (谢谢)
What's happening here ...
ParameterType.RequestBody
只能添加一次,因为请求只有一个BODY。所有后续添加都将被忽略。
Request Body
If this parameter is set, its value will be sent as the body of the request. Only one
RequestBody
parameter is accepted - the first one.
注意:强调我的。
这证实了所提供的原始示例中所展示的问题。
... and how do I fix this issue?
在这种情况下使用 ParameterType.GetOrPost
private IRestResponse CallBox(string url, Method method, KeyValuePair<string, string>[] parameters = null, bool isUpload = false) {
parameters = parameters ?? new KeyValuePair<string, string>[] { };
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(method);
foreach (KeyValuePair<string, string> parameter in parameters) {
request.AddParameter(parameter.Key, parameter.Value, ParameterType.GetOrPost);
}
IRestResponse response = client.Execute(request);
return response;
}
Get or Post
This behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in the form
url?name1=value1&name2=value2
.On a POST or PUT Requests, it depends on whether or not you have files attached to a Request. If not, the Parameters will be sent as the body of the request in the form
name1=value1&name2=value2
. Also, the request will be sent asapplication/x-www-form-urlencoded
.In both cases, name and value will automatically be url-encoded.
If you have files, RestSharp will send a
multipart/form-data
request. Your parameters will be part of this request in the form:Content-Disposition: form-data; name="parameterName" ParameterValue
注意:强调我的。
我认为你应该修改你的代码:
foreach (KeyValuePair<string, string> param in parameters)
{
Require.Argument(param.Key, param.Value);
request.AddParameter(new Parameter(param.Key, param.Value, ParameterType.RequestBody));
}