多个 AddParameter 调用只添加第一个参数,忽略其余参数

Multiple AddParameter calls only add the first parameter, ignoring the rest

相关项目信息:

我查看了 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。所有后续添加都将被忽略。

引用Documentation

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;
}

引用Documentation

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));
}