0

Is it correct to quote the boundary property value of Content-Type header?

I have sent an http-request with two files to a third-party server and get the following response:

Boundary '--"38b14895-fd44-4acc-8287-9f0378691da2"' not found in message body

because RestSharp quotes the boundary value, but the server doesn't unquote it. I can neither change the third-party server nor customize RestSharp header quoting.

What is the problem? Does the http spec allow escaped strings in header property values? I've read the spec, but haven't found a place where this would be explicitly defined.

I create the RestRequest something like this:

  private RestRequest CreateRequest( ... )
  {
    var request_url = $"url?param=value";
    var request = new RestRequest( request_url, Method.Post );

    request.AddFile( "file1", ..., "file1", "application/xml" );
    request.AddFile( "file2", ..., "file2", "audio/x-wav" );

    request.AddHeader( "Content-Type", "multipart/form-data" );

    return request;
  }

and get the following HTTP-request:

POST /url?param=value
Host: 192.168.1.1:80
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/108.0.1.0
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary="38b14895-fd44-4acc-8287-9f0378691da2"
Content-Length: 227841

--38b14895-fd44-4acc-8287-9f0378691da2
Content-Type: application/xml
Content-Disposition: form-data; name="file1"; filename="file1"

[data]

--38b14895-fd44-4acc-8287-9f0378691da2
Content-Type: audio/x-wav
Content-Disposition: form-data; name="file2"; filename="file2"

[data]

--38b14895-fd44-4acc-8287-9f0378691da2--

exGens
  • 1
  • 1

1 Answers1

0

Okay so I have just been dealing with a very similar issue. I found this GitHub Issue which gives some good context into the actual issue that is occurring here but I have also found a fix.

Firstly, you shouldn't manually add the "Content-Type" header. RestSharp will do this for you since you are using the AddFile() method.

I am assuming you are using the latest version of RestSharp, if so you can set the request.OnBeforeRequest property of the RestRequest to handle this and strip out the double quotes around the boundary before the request is sent:

request.OnBeforeRequest = (http) =>
{
    var boundary = http.Content.Headers.ContentType.Parameters.First(o => o.Name == "boundary");
    boundary.Value = boundary.Value.Replace("\"", String.Empty);
    return default;
};

Hope this helps!