MultipartFormDataContent.Add StringContent is adding carraige return/linefeed to the name

走远了吗. 提交于 2021-01-27 14:26:34

问题


formData.Add(sJobId,"job_id"); is sending "job_id\r\n" to the server

Here is my C# method:

public static async Task UploadAsync(string url, int job_id, string filename, string filePath) {
  try {
      // Submit the form using HttpClient and 
      // create form data as Multipart (enctype="multipart/form-data")
      using (var fileStream = new StreamContent(System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read)))
      using (var formData = new MultipartFormDataContent()) {
          StringContent sJobId = new StringContent(job_id.ToString());
          StringContent sOthId = new StringContent(job_id.ToString());
          // Try as I might C# adds a CrLf to the end of the job_id tag - so have to strip it in ruby
          formData.Add(sOthId, "oth_id");
          formData.Add(sJobId,"job_id");
          fileStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
          formData.Add(fileStream, "dt_file", filename);
          HttpResponseMessage response = await HttpClient.PostAsync(url, formData);

          // If the upload failed there is not a lot we can do 
          return;
      }
  } catch (Exception ex) {
      // Not a lot we can do here - so just ignore it
      System.Diagnostics.Debug.WriteLine($"Upload failed {ex.Message}");
  }

}

This is what my Ruby puma server is receiving - see how oth_id and job_id have \r\n appended but "dt_file" does not.

Parameters: {"oth_id\r\n"=>"42157", "job_id\r\n"=>"42157", "dt_file"=>#<ActionDispatch::Http::UploadedFile:0x007f532817dc98 @tempfile=#<Tempfile:/tmp/RackMultipart20190715-37897-189ztb6.msg>, @original_filename="2019-07-15 164600.msg", @content_type="application/octet-stream", @headers="Content-Type: application/octet-stream\r\nContent-Disposition: form-data; name=dt_file; filename=\"2019-07-15 164600.msg\"; filename*=utf-8''2019-07-15%20164600.msg\r\n">}

How do I stop the formData.Add appending a \r\n to the name?


回答1:


The raw message the application is sending to the server is

POST https://example.com/ HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary="93655e5a-b6b3-48d6-82c9-0d9aa99164cc"
Content-Length: 522

--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=oth_id

1234
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=job_id

1234
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: application/octet-stream
Content-Disposition: form-data; name=dt_file; filename=myfile.txt; filename*=utf-8''myfile.txt

a,b,c,d
aa,"b,b","c
c",dd
aaa
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc--

Have a look at the name values.

Looking at RFC 7578 I can see in every example, that the value for name is always quoted.

Content-Disposition: form-data; name="user"

I did not find any hint if it is mandantory or not to quote the values, so I cannot judge who is wrong here.

To get such quoted name values you only have to quote the values in code.

public static async Task UploadAsync(string url, int job_id, string filename, string filePath) {
  try {
      // Submit the form using HttpClient and 
      // create form data as Multipart (enctype="multipart/form-data")
      using (var fileStream = new StreamContent(System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read)))
      using (var formData = new MultipartFormDataContent()) {
          StringContent sJobId = new StringContent(job_id.ToString());
          StringContent sOthId = new StringContent(job_id.ToString());
          // Try as I might C# adds a CrLf to the end of the job_id tag - so have to strip it in ruby
          formData.Add(sOthId, "\"oth_id\"");
          formData.Add(sJobId,"\"job_id\"");
          fileStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
          formData.Add(fileStream, "\"dt_file\"", filename);
          HttpResponseMessage response = await HttpClient.PostAsync(url, formData);

          // If the upload failed there is not a lot we can do 
          return;
      }
  } catch (Exception ex) {
      // Not a lot we can do here - so just ignore it
      System.Diagnostics.Debug.WriteLine($"Upload failed {ex.Message}");
  }

}

which will now post this

POST https://example.com/ HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary="c33cdc86-db44-40ef-8e6e-3e13a96218d1"
Content-Length: 528

--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="oth_id"

1234
--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="job_id"

1234
--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: application/octet-stream
Content-Disposition: form-data; name="dt_file"; filename=myfile.txt; filename*=utf-8''myfile.txt

a,b,c,d
aa,"b,b","c
c",dd
aaa
--c33cdc86-db44-40ef-8e6e-3e13a96218d1--

Just found a pull request for PowerShell

// .NET does not enclose field names in quotes, however, modern browsers and curl do.
contentDisposition.Name = $"\"{LanguagePrimitives.ConvertTo<String>(fieldName)}\"";


来源:https://stackoverflow.com/questions/57033535/multipartformdatacontent-add-stringcontent-is-adding-carraige-return-linefeed-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!