cpr icon indicating copy to clipboard operation
cpr copied to clipboard

The uploaded file has some strange content ?

Open anjisZhou opened this issue 3 years ago • 9 comments

This is my upload code, uploaded to Amazon's server via http auto r = cpr::Put(url, cpr::Multipart{{"file", cpr::File{_filePath}}}, cpr::ProgressCallback([&](size_t downloadTotal, size_t downloadNow, size_t uploadTotal, size_t uploadNow) -> bool { return true; })); Why does the file I upload to the server have these contents? 图片

anjisZhou avatar Jul 15 '21 12:07 anjisZhou

This looks rather interesting and should not be there :D What software do you use at the receiver end, since I'm not able to reproduce this. What version of CPR are you using? What platform are you on (Windows, Linux, ...)?

COM8 avatar Jul 16 '21 06:07 COM8

I am using Amazon's file server and I don't know it very well. It feels that it does not support "boundary", I just used this method to upload successfully

auto r = cpr::Put(url, cpr::Body(fileData.data(),fileData.size()), cpr::Header{{"content-type","application/octet-stream"}});

anjisZhou avatar Jul 16 '21 07:07 anjisZhou

I'm currently unable to reproduce this issue. I'm using the following test case:

TEST(FileUpload, FormPostSingleTest) {
    Url url{"http://httpbin.org/put"};
    Response response = cpr::Put(url, Multipart{{"someFile", cpr::File{"/home/fabian/test.txt"}}});
    std::cout << response.text;
    EXPECT_EQ(url, response.url);
    EXPECT_EQ(200, response.status_code);
    EXPECT_EQ(ErrorCode::OK, response.error.code);
}

Which then results in the following with the correct file content.

{
  "args": {}, 
  "data": "", 
  "files": {
    "someFile": "Hello!\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "deflate, gzip", 
    "Content-Length": "197", 
    "Content-Type": "multipart/form-data; boundary=------------------------849ce773b180b64b", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.75.0", 
    "X-Amzn-Trace-Id": "Root=1-60fea159-797b814b42c9fb3d3c11d650"
  }, 
  "json": null, 
  "origin": "131.159.24.141", 
  "url": "http://httpbin.org/put"
}

COM8 avatar Jul 26 '21 11:07 COM8

Can anyone provide me with a small example project, so I can reproduce this error?

COM8 avatar Aug 03 '21 08:08 COM8

yes, i met this before, i got strange content at the beginning and end of the file-content:

--------------------------9add7af2b74072ba Content-Disposition: form-data; name="someFile"; filename="main.cpp" Content-Type: application/octet-stream

#include <iostream>

#pragma comment(lib, "libcurl-d.lib")

#include <cpr/cpr.h>
#include <fstream>

int main(int argc, char* args[])
{
    {
        //cpr::Url url{ "http://httpbin.org/put" };
        cpr::Url url{ "http://localhost/testFile.cpp" };
        cpr::Response response = cpr::Put(url, cpr::Multipart{
            {"someFile", cpr::File{"./main.cpp"}} });
        std::cout << response.text;
        getchar();
    }
}

--------------------------9add7af2b74072ba--

lsaejn avatar Aug 13 '21 02:08 lsaejn

Can anyone provide me with a small example project, so I can reproduce this error?

i'm using nginx-1.12.2, windows. i modify nginx.conf to support PUT method by adding the following contents:

 dav_methods  PUT;

lsaejn avatar Aug 23 '21 05:08 lsaejn

Any updates on this? I am uploading wave files to GCP and the contents of the file have information on multipart. Therefore, we have failures while reproducing waves.

image

Edit:

More context:

  • LibCPR version: 1.10.0
  • OS: Mac OS Ventura 13.2

caiomcg avatar Aug 25 '23 13:08 caiomcg

Any updates on this? I am uploading wave files to GCP and the contents of the file have information on multipart. Therefore, we have failures while reproducing waves.

image Edit:

More context:

  • LibCPR version: 1.10.0
  • OS: Mac OS Ventura 13.2

Upon a quick investigation I have concluded that LibCPR is doing the correct approach, the problem relies within GCS as it doesn't handle Multipart. I changed my approach to send the entire file in the body of my request.

Reference:

  • https://stackoverflow.com/questions/57750518/why-do-files-uploaded-with-curl-contain-part-of-the-request-header-like-bounda

caiomcg avatar Aug 25 '23 14:08 caiomcg

@caiomcg thanks for looking into this and providing a ref. solution. Does this mean, we need to change anything on the cpr side of things since as far as I can see it, it's a server side issue and therefore not something we can fix?

COM8 avatar Aug 27 '23 09:08 COM8