cpr icon indicating copy to clipboard operation
cpr copied to clipboard

Allow Clearing Session Content Via E.G. `cpr::Session::ClearContent();`

Open DanielRe1 opened this issue 1 year ago • 1 comments

Description

I'm using a cpr::Session to perform multiple HTTP Requests. At first I want to do a HTTP POST with a multipart parameter. After that I want to do a simple GET.

It seems like cpr always performs a POST when a multipart parameter has been added to the session. This is because the sessions prepareCommon adds the content to each request once it was set for the session.

    cpr::Session session;
    session.SetUrl(  ...  );
    session.SetMultipart( ...  );
    session.Post( );
    session.Get( );     <-- This is also a POST

Is this the desired beaviour? Is there any workaround to remove the content from the session after the POST?

Example/How to Reproduce

  1. Create a cpr::Session
  2. Set Mulipart parameter
  3. Perform POST
  4. Perform GET

Possible Fix

No response

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

  • OS: Linux
  • Version: 1.11.0

DanielRe1 avatar Jan 10 '25 13:01 DanielRe1

@DanielRe1 thanks for reporting! Sadly there is no way right now to clear the underlying std::variant<std::monostate, cpr::Payload, cpr::Body, cpr::Multipart> content_{std::monostate{}}; object back to std::monostate{}.

But what you could try is:

session.SetBody(cpr::Body{});

That way you at least clear the multipart.

Is this intended behaviour? I'd say right now yes. But I would be open to merging a MR that adds a cpr::Session::ClearContent(); (or called differently) call to session.

COM8 avatar Jan 12 '25 18:01 COM8