Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post data of cpr::File is different from a cpr::Buffer of the same file #1105

Open
vancer0 opened this issue Sep 6, 2024 · 0 comments
Open

Comments

@vancer0
Copy link

vancer0 commented Sep 6, 2024

Description

When I try to upload a file within a Multipart with a Post request, the bytes are different when I use cpr::File and cpr::Buffer for the same file. Below is some example code I tried with a small PNG image.

// Using Buffer
std::vector<char> vec;
std::ifstream file("test.png");
std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(vec));

cpr::Response res1 = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                               cpr::Multipart{{"name", cpr::Buffer{vec.begin(), vec.end(), "test.png"}}});
std::cout << res1.text << std::endl;

// Using File
cpr::Response res2 = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                               cpr::Multipart{{"name", cpr::File{"test.png"}}});
std::cout << res2.text << std::endl;

The output I get is this:

{
  "args": {}, 
  "data": "", 
  "files": {
    "name": "<some data>"
  }, 
  "form": {}, 
  "headers": {
    ...
    "Content-Length": "25654", 
    "Content-Type": "multipart/form-data; boundary=------------------------7XWq6nScPC3xKHD7Jjb9z2", 
    "User-Agent": "curl/8.6.0-DEV", 
    ...
  }, 
  "json": null, 
  "origin": "...", 
  "url": "http://www.httpbin.org/post"
}

{
  "args": {}, 
  "data": "", 
  "files": {
    "name": "<different data>"
  }, 
  "form": {}, 
  "headers": {
    ...
    "Content-Length": "28374", 
    "Content-Type": "multipart/form-data; boundary=------------------------btaObLMBqn5Laa0AYu2ytO", 
    "User-Agent": "curl/8.6.0-DEV", 
    ...
  }, 
  "json": null, 
  "origin": "...", 
  "url": "http://www.httpbin.org/post"
}

You can also see that the lengths are different. Also I should mention that if I save the vec buffer back again into a file, it is identical to the original.

Example/How to Reproduce

See code example

Possible Fix

No response

Where did you get it from?

vcpkg

Additional Context/Your Environment

  • OS: Fedora Linux
  • Kernel Version: 6.10.6
  • cURL Version: 8.6.0 vcpkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant