Send user specified headers with the request
This commit is contained in:
parent
74e98211a7
commit
bc6b20563b
3 changed files with 17 additions and 3 deletions
|
@ -20,6 +20,7 @@
|
||||||
#include <curl_easy.h>
|
#include <curl_easy.h>
|
||||||
#include <curl_pair.h>
|
#include <curl_pair.h>
|
||||||
#include <curl_ios.h>
|
#include <curl_ios.h>
|
||||||
|
#include <curl_header.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -38,7 +39,8 @@ namespace {
|
||||||
|
|
||||||
HttpResponse page_fetch (
|
HttpResponse page_fetch (
|
||||||
const std::string& url,
|
const std::string& url,
|
||||||
const std::string& user_agent
|
const std::string& user_agent,
|
||||||
|
const PageFetchHeaders& headers
|
||||||
) {
|
) {
|
||||||
using curl::curl_pair;
|
using curl::curl_pair;
|
||||||
|
|
||||||
|
@ -62,6 +64,12 @@ HttpResponse page_fetch (
|
||||||
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
|
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
|
||||||
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent));
|
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();
|
easy.perform();
|
||||||
|
|
||||||
HttpResponse resp;
|
HttpResponse resp;
|
||||||
|
|
|
@ -19,12 +19,18 @@
|
||||||
|
|
||||||
#include "nap/http_response.hpp"
|
#include "nap/http_response.hpp"
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace nap {
|
namespace nap {
|
||||||
|
|
||||||
|
typedef std::vector<std::pair<std::string, std::string>> PageFetchHeaders;
|
||||||
|
|
||||||
HttpResponse page_fetch (
|
HttpResponse page_fetch (
|
||||||
const std::string& url,
|
const std::string& url,
|
||||||
const std::string& user_agent
|
const std::string& user_agent,
|
||||||
|
const PageFetchHeaders& headers
|
||||||
);
|
);
|
||||||
|
|
||||||
} //namespace nap
|
} //namespace nap
|
||||||
|
|
|
@ -32,6 +32,6 @@ void QuickRest::set_user_agent (std::string&& name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse QuickRest::fetch (std::string_view url) {
|
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
|
} //namespace nap
|
||||||
|
|
Loading…
Add table
Reference in a new issue