Skip to content

Can't cancel an async request v1.12.0 ... v1.14.1 #1283

@baderouaich

Description

@baderouaich

Description

This syntax doesn't compile anymore in v1.12.0 and up:

#include <iostream>
#include <cpr/cpr.h>

int main() {
  // Launch a long async GET request...
  cpr::Session session{};
  session.SetUrl(cpr::Url{"https://ash-speed.hetzner.com/1GB.bin"});
  cpr::AsyncResponse future = session.GetAsync();

  // Wait 1s  
  std::this_thread::sleep_for(std::chrono::seconds(1));

  // Cancel request after 1s
  if (not future.IsCancelled()) {
    std::cout << "Cancelling..." << std::endl;
    cpr::CancellationResult cr = future.Cancel();
    switch (cr) {
      case cpr::CancellationResult::failure:
        std::cout << "failure" << std::endl;
        break;
      case cpr::CancellationResult::success:
        std::cout << "success" << std::endl;
        break;
      case cpr::CancellationResult::invalid_operation:
        std::cout << "invalid_operation" << std::endl;
        break;
    }
  }
}

Compile error

/home/libcpr_cancelable/main.cpp: In function ‘int main()’:
/home/libcpr_cancelable/main.cpp:11:18: error: ‘using cpr::AsyncResponse = class cpr::AsyncWrapper<cpr::Response>’ {aka ‘class cpr::AsyncWrapper<cpr::Response>’} has no member named ‘IsCancelled’
   11 |   if (not future.IsCancelled()) {
      |                  ^~~~~~~~~~~
/home/libcpr_cancelable/main.cpp:13:41: error: ‘using cpr::AsyncResponse = class cpr::AsyncWrapper<cpr::Response>’ {aka ‘class cpr::AsyncWrapper<cpr::Response>’} has no member named ‘Cancel’
   13 |     cpr::CancellationResult cr = future.Cancel();
      |                                         ^~~~~~

Did the api change recently? if so what will be the new way of cancelling async requests? Thank you.

Example/How to Reproduce

  1. Create a cpr::Session
  2. Set option cpr::Url("https://ash-speed.hetzner.com/1GB.bin") to slow down the response
  3. Perform an async GET request
  4. Cancel the async request after ~1s
  5. Compile and see compilation error

Possible Fix

No response

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

  • OS: Ubuntu
  • Version: 24.04.1 LTS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions