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

I'm trying to write a call to the Freshdesk API using either Flurl or RestSharp library.

The API call I'm trying to write is Creating a Ticket: https://developers.freshdesk.com/api/#create_ticket

An example of cURL:

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

Note params are passed by -F (i.e. --form) flag.

With the following code calling 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);
                  });

...Or the following code calling 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);

...I'm receiving 400 BadRequest with the following information (in both cases):

{
    "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"
        }
    ]
}

Note that:

  • Freshdesk API uses -F / --form as a way to pass parameters.
  • I want to use Multipart for handling attachments later on.
  • All data has proper values. If it wouldn't have a proper value, a validation error would come as a response.
  • When sending the same values in Postman (via form-data), I'm receiving 201 Created.

Update 1

Postman screenshot (working example):

enter image description here

Update 2

I tried to use Tiny.RestClient which has cURL listener and it came with the following cURL generated:

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

[email protected]
[...]

Perhaps, Flurl and RestSharp uses the same way to proceed with parameters and thus, the \r\n in fields.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source