Giter Club home page Giter Club logo

Comments (4)

Chrys4lisfag avatar Chrys4lisfag commented on June 7, 2024 1

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.

COM8 avatar COM8 commented on June 7, 2024

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.

Chrys4lisfag avatar Chrys4lisfag commented on June 7, 2024

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.

COM8 avatar COM8 commented on June 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.