Add check for http status, add post method
This commit is contained in:
@ -1,16 +1,9 @@
|
|||||||
//
|
|
||||||
// requester.cpp
|
|
||||||
// curly
|
|
||||||
//
|
|
||||||
// Created by Bob Polis at 2020-09-01
|
|
||||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "requester.hpp"
|
#include "requester.hpp"
|
||||||
#include <libsclogging.hpp>
|
#include <curl/curl.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <libsclogging.hpp>
|
||||||
|
|
||||||
extern sc::logger logger;
|
extern sc::logger logger;
|
||||||
|
|
||||||
@ -38,5 +31,27 @@ std::string requester::get(const std::string &url) {
|
|||||||
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &text);
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &text);
|
||||||
auto success = curl_easy_perform(_h.get());
|
auto success = curl_easy_perform(_h.get());
|
||||||
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
||||||
|
long status;
|
||||||
|
success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &status);
|
||||||
|
if (success != CURLE_OK || status != 200) throw std::runtime_error("could not get remote data");
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string requester::post(const std::string& url, const std::string& json) {
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_POST, 1L);
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_POSTFIELDS, json.c_str());
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_POSTFIELDSIZE, json.size());
|
||||||
|
|
||||||
|
struct curl_slist* headers {nullptr};
|
||||||
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||||
|
std::unique_ptr<struct curl_slist, void(*)(struct curl_slist*)> hdrs {headers, curl_slist_free_all};
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_HTTPHEADER, headers);
|
||||||
|
|
||||||
|
std::string response;
|
||||||
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &response);
|
||||||
|
|
||||||
|
auto success = curl_easy_perform(_h.get());
|
||||||
|
if (success != CURLE_OK) throw std::runtime_error("could not post data");
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
@ -1,11 +1,3 @@
|
|||||||
//
|
|
||||||
// requester.hpp
|
|
||||||
// curly
|
|
||||||
//
|
|
||||||
// Created by Bob Polis at 2020-09-01
|
|
||||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef _requester_H_
|
#ifndef _requester_H_
|
||||||
#define _requester_H_
|
#define _requester_H_
|
||||||
|
|
||||||
@ -31,6 +23,9 @@ class requester {
|
|||||||
// perform a http get request
|
// perform a http get request
|
||||||
std::string get(const std::string& url);
|
std::string get(const std::string& url);
|
||||||
|
|
||||||
|
// perform a http post request
|
||||||
|
std::string post(const std::string& url, const std::string& json);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<CURL, void(*)(CURL*)> _h {nullptr, curl_easy_cleanup};
|
std::unique_ptr<CURL, void(*)(CURL*)> _h {nullptr, curl_easy_cleanup};
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user