Skip to content

Add operator to fetch cookies by their key - #1312

Merged
COM8 merged 2 commits into
libcpr:masterfrom
Callen44:master
Jul 26, 2026
Merged

Add operator to fetch cookies by their key#1312
COM8 merged 2 commits into
libcpr:masterfrom
Callen44:master

Conversation

@Callen44

@Callen44 Callen44 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

The main idea of this PR is to add the ability to fetch cookies by their individual key from a cpr::Cookies object. calling the [] operator on a string storing the cookie name would return the actual cookie with that name, if not, it throws std::out_of_range. Looping through the list of cookies produces annoying boilerplate code, and this would fix that. I'm a new C++ programmer; worse yet, my introduction to C++ was with version 98, so I won't be offended by quite possibly a lot of feedback.

I already have these concerns with my own PR, however.

  1. The idea of a Cookies object is to be vector-like, and this code intentionally more map-like; I'm not sure if the very concept is out-of-place or not.
  2. Looping through cookies like this could become very inefficient for large cookie lists (I'm not sure if that kind of speed is a priority in cpr or not).
  3. There's always the problem of a given cookie not being possible to find, I'd love some help on whether throwing std::out_of_range is the right way to handle that.

@COM8 COM8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Callen44 thanks for contributing. Only one comment.

Comment thread cpr/cookies.cpp Outdated
Comment on lines +78 to +82
for (size_t i = 0; i < cookies_.size(); i++) { // NOLINT(*-loop-convert)
if (cookies_[i].GetName() == key) {
return cookies_[i];
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use std::find_if. It should be available in C++17 (https://en.cppreference.com/cpp/algorithm/find).

@Callen44 Callen44 Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are my changes satisfactory?

@COM8 COM8 added this to the CPR 1.15.0 milestone Jul 19, 2026
@Callen44
Callen44 requested a review from COM8 July 24, 2026 19:25

@COM8 COM8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Callen44 Thanks!

@COM8
COM8 merged commit b701833 into libcpr:master Jul 26, 2026
31 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants