4

I am trying to consume a REST service using TRestClient but I believe there is an issue with the boundary string for multipart content.

I am capturing the body of the request I am sending, and this is the content type header:

Content-Type: multipart/form-data; boundary=-------Embt-Boundary--07CC944C29DA577E

Then, this is the first section of the multipart form:

-----------Embt-Boundary--07CC944C29DA577E
Content-Disposition: form-data; name="file"; filename="ce.csv"
Content-Type: text/csv

And this is how it ends:

---------Embt-Boundary--07CC944C29DA577E--

I don't think this is an issue on the server, as even my proxy is not able to parse the body:

image

When I compare this same request vs postman, I notice that the starting and ending boundaries do not match!

Starting: -----------Embt-Boundary--07CC944C29DA577E
Ending:   ---------Embt-Boundary--07CC944C29DA577E-- 

I found that the boundary generation is done in TMultipartFormData.GenerateBoundary() from System.Net.Mime:

image

When checking the starting and ending boundaries from postman, they match, so I am almost sure this is the issue. I don't think it is related to my code, but let me know if you need it.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Miguel Febres
  • 2,153
  • 2
  • 21
  • 31
  • 2
    The starting boundary on the MIME body you are capturing is wrong. Whatever value is specified in the `boundary` attribute of the `Content-Type` header, each boundary in the body must be prefixed with `--`, and the last boundary must be suffixed with `--`. IOW, given `boundary=`, each starting boundary is `--` and the ending boundary is `----`. But in your case, the starting boundary is `----` instead, which is wrong (the ending boundary is fine). So this is clearly a bug in `TRestClient` and should be [reported to Embarcadero](https://quality.embarcadero.com). – Remy Lebeau Mar 31 '20 at 23:55
  • @RemyLebeau Indeed, seems to be a bug in System.Net.Mime TMultipartFormData.AdjustLastBoundary. By changing FStream.Position := FStream.Size - (Length(FBoundary) + 4) to FStream.Position := FStream.Size - (Length(FBoundary) + 6) to include that 2 additional dashes issue is fixed. Link to the report: https://quality.embarcadero.com/browse/RSP-27968 – Miguel Febres Apr 01 '20 at 00:46

0 Answers0