Send user specified headers with the request

This commit is contained in:
King_DuckZ 2020-08-29 14:30:08 +01:00
parent 74e98211a7
commit bc6b20563b
3 changed files with 17 additions and 3 deletions

View file

@ -20,6 +20,7 @@
#include <curl_easy.h>
#include <curl_pair.h>
#include <curl_ios.h>
#include <curl_header.h>
#include <sstream>
#include <algorithm>
#include <cassert>
@ -38,7 +39,8 @@ namespace {
HttpResponse page_fetch (
const std::string& url,
const std::string& user_agent
const std::string& user_agent,
const PageFetchHeaders& headers
) {
using curl::curl_pair;
@ -62,6 +64,12 @@ HttpResponse page_fetch (
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent));
curl::curl_header ch;
for (const auto& entry : headers) {
ch.add(entry.first + ": " + entry.second);
}
easy.add<CURLOPT_HTTPHEADER>(ch.get());
easy.perform();
HttpResponse resp;

View file

@ -19,12 +19,18 @@
#include "nap/http_response.hpp"
#include <string_view>
#include <string>
#include <vector>
#include <utility>
namespace nap {
typedef std::vector<std::pair<std::string, std::string>> PageFetchHeaders;
HttpResponse page_fetch (
const std::string& url,
const std::string& user_agent
const std::string& user_agent,
const PageFetchHeaders& headers
);
} //namespace nap

View file

@ -32,6 +32,6 @@ void QuickRest::set_user_agent (std::string&& name) {
}
HttpResponse QuickRest::fetch (std::string_view url) {
return page_fetch(std::string(url), m_user_agent);
return page_fetch(std::string(url), m_user_agent, m_header);
}
} //namespace nap