Flurl \ RestSharp: `\r\n` 被添加到 Multipart 请求的每个参数中

Flurl \ RestSharp: `\r\n` is added to each parameter of the Multipart request

我正在尝试使用 Flurl 或 RestSharp 库编写对 Freshdesk API 的调用。

我正在尝试编写的 API 调用是创建工单:https://developers.freshdesk.com/api/#create_ticket

cURL 示例:

curl -v -u yourapikey:X -F "attachments[]=@/path/to/attachment1.ext" -F "attachments[]=@/path/to/attachment2.ext" -F "email=example@example.com" -F "subject=Ticket Title" -F "description=this is a sample ticket" -X POST 'https://domain.freshdesk.com/api/v2/tickets'

注意参数由 -F(即 --form)标志传递。

使用以下代码调用 Flurl:

var rs = await url.WithBasicAuth(_configuration.ApiKey, _configuration.Password)
                  .PostMultipartAsync(mp =>
                  {
                      mp.AddString("email", ticket.Email);
                      mp.AddString("subject", ticket.Subject);
                      mp.AddString("status", ticket.Status.ToString());
                      mp.AddString("priority", ticket.Priority.ToString());
                      mp.AddString("description", ticket.Description);
                  });

...或调用 RestSharp 的以下代码:

var request = new RestRequest("tickets", Method.Post);
request.AlwaysMultipartFormData = true;
request.AddParameter("email", ticket.Email);
request.AddParameter("description", ticket.Description);
request.AddParameter("subject", ticket.Subject);
request.AddParameter("status", ticket.Status);
request.AddParameter("priority", ticket.Priority);

var rs = await client.ExecuteAsync(request);

...我收到了包含以下信息的 400 BadRequest(在两种情况下):

{
    "description": "Validation failed",
    "errors": [
        {
            "code": "invalid_field",
            "field": "email\r\n",
            "message": "Unexpected/invalid field in request"
        },
        {
            "code": "invalid_field",
            "field": "subject\r\n",
            "message": "Unexpected/invalid field in request"
        },
        {
            "code": "invalid_field",
            "field": "status\r\n",
            "message": "Unexpected/invalid field in request"
        },
        {
            "code": "invalid_field",
            "field": "priority\r\n",
            "message": "Unexpected/invalid field in request"
        },
        {
            "code": "invalid_field",
            "field": "description\r\n",
            "message": "Unexpected/invalid field in request"
        }
    ]
}

注意:

更新 1

Postman 屏幕截图(工作示例):

更新 2

我尝试使用具有 cURL 侦听器的 Tiny.RestClient,它附带生成了以下 cURL:

curl -X POST [...] -d "--1c3acb07-77bd-494d-bc56-21290dcd5088
Content-Type: text/plain
Content-Disposition: form-data; name=email

a@a.com
[...]

也许,Flurl 和 RestSharp 使用相同的方式处理参数,因此,字段中的 \r\n

这是 FreshDesk API 问题。出于某种原因,他们希望将 multipart form-data 参数名称括在引号中。这不是 HTTP 标准的要求。 RestSharp 和 Flurl 使用 MultipartFormDataContent,并添加使用提供的参数名称。

根据您的问题,我将此 属性 添加到 RestRequest

/// <summary>
/// When set to true, parameters in a multipart form data requests will be enclosed in
/// quotation marks. Default is false. Enable it if the remote endpoint requires parameters
/// to be in quotes (for example, FreshDesk API). 
/// </summary>
public bool MultipartFormQuoteParameters { get; set; }

它在 RS 107.3.0 中可用。

您也可以通过添加如下参数使其工作:

request.AddParameter("\"status\"", ticket.Status);