Comments (4)
PostCallback
does not perform the session preparation and therefore the copying directly. The request gets enqueued and performed asynchronously. It could get executed immediately, or it can be delayed.In your case make sure, your buffer is valid until your callback gets invoked.
Thanks. Now it is clear for me.
from cpr.
Yes, you have to ensure your memory stays valid when using cpr::Buffer
. We only store a pointer but as soon as you call for example cpr::Get
or an async version of it your memory location cpr::Buffer
is pointing to is copied into curl (curl_mime_data
).
From then on you are safe to reuse your memory cpr::Buffer
is pointing to except if you want to call a further request on the same session. Every time you call cpr::Get
or its async counterpart, cpr copies the data pointed to from your passed cpr::Buffer
into curl again to prepare the next request.
from cpr.
Didn't quite get the meaning of this message " We only store a pointer but as soon as you call for example cpr::Get or an async version of it your memory location cpr::Buffer is pointing to is copied"
__declspec( noinline ) void send_file()
{
std::ifstream file( "file.json", std::ios::binary );
std::vector<char> buffer( std::istreambuf_iterator<char>( file ), {} );
const cpr::Url req_uri( "https://api.telegram.org/" + token + "/sendDocument");
const cpr::Redirect red{ 52L, true, true, cpr::PostRedirectFlags::POST_ALL };
const cpr::Timeout timeout{ 50000 };
const cpr::Multipart payload{ { "chat_id", chat },
{ "caption", "test"},
{ "document", cpr::Buffer{ buffer.begin(), buffer.end(), std::string( "test.json" ) } } };
auto r = cpr::PostCallback( []( const cpr::Response& resp )
{
if ( resp.status_code != 200 )
{
std::cout << "Error: " << resp.status_code << std::endl;
std::cout << "Error: " << resp.text << std::endl;
std::cout << "Error: " << resp.error.message << std::endl;
}
}, req_uri, payload, red, timeout );
r.get();
}
You say that when PostCallback is called, my buffer content should be copied.
In my test case if I do r.get(); (block until resp), I recv valid file. If I comment this then I get broken file (buffer memory destructed).
If the buffer content was copied right after PostCallback called, then it shouldn't have happened. Or you meant that buffer is copied somewhere later inside when task runner starts the request (at that moment buffer scope exited ) and passed ptr to curl and that is the thing I didn't understand in your answer.
from cpr.
PostCallback
does not perform the session preparation and therefore the copying directly. The request gets enqueued and performed asynchronously.
It could get executed immediately, or it can be delayed.
In your case make sure, your buffer is valid until your callback gets invoked.
from cpr.
Related Issues (20)
- Built-in curl link to libssh2 automatically HOT 8
- Failed to build with ndk HOT 7
- cpr::Download failed to decode downloaded content of gzip encoding HOT 4
- trying to use discord webhook api: St12length_error - basic_string::_M_create HOT 1
- CMake target exports are not created if not using system curl HOT 3
- Handling SSE HOT 3
- build error for android using cmake on macOS HOT 1
- HTTPS segmentation fault, but HTTP will not. HOT 2
- Link error when setting -DCMAKE_BUILD_TYPE=Debug HOT 4
- SetUrl() does not update new URLs properly within same Session() object HOT 1
- SSL certificate pinning HOT 3
- Question: Is `WriteCallback` asynchronous? HOT 1
- constexpr methods for some string-like types HOT 1
- Add support for identifying whether a Session is currently busy or not HOT 1
- zlib ignores --prefix/CMAKE_INSTALL_PREFIX when using cpr as external dependency HOT 2
- Link error when creating a cpr::Header, segment fault when making cpr::Get request HOT 1
- Question: Does cpr support ftp? HOT 1
- Confused on something HOT 3
- Call of overloaded ‘SetOption(std::__cxx11::basic_string<char>)’ is ambiguous HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpr.