How do you make a HTTP request with C++?

后端 未结 22 2779
有刺的猬
有刺的猬 2020-11-22 06:25

Is there any way to easily make a HTTP request with C++? Specifically, I want to download the contents of a page (an API) and check the contents to see if it contains a 1 o

22条回答
  •  不知归路
    2020-11-22 07:04

    You can use embeddedRest library. It is lightweight header-only library. So it is easy to include it to your project and it does not require compilation cause there no .cpp files in it.

    Request example from readme.md from repo:

    #include "UrlRequest.hpp"
    
    //...
    
    UrlRequest request;
    request.host("api.vk.com");
    const auto countryId = 1;
    const auto count = 1000;
    request.uri("/method/database.getCities",{
        { "lang", "ru" },
        { "country_id", countryId },
        { "count", count },
        { "need_all", "1" },
    });
    request.addHeader("Content-Type: application/json");
    auto response = std::move(request.perform());
    if (response.statusCode() == 200) {
      cout << "status code = " << response.statusCode() << ", body = *" << response.body() << "*" << endl;
    }else{
      cout << "status code = " << response.statusCode() << ", description = " << response.statusDescription() << endl;
    }
    

提交回复
热议问题