相关项目信息:
- 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
注意:强调我的。
关于c# - 多个 AddParameter 调用只添加第一个参数,忽略其余参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59683268/