17 #include <cpprest/filestream.h> 18 #include <cpprest/http_client.h> 25 using file_buffer = concurrency::streams::file_buffer<uint8_t>;
26 using streambuf = concurrency::streams::streambuf<uint8_t>;
30 m_ClientConfig.set_validate_certificates(
false);
37 auto status = response.status_code();
46 auto requestContentType = response.headers().content_type();
49 if (requestContentType.find(U(
"json")) != std::wstring::npos)
52 <<
"' but contains 'json'. So we awesome the answer actually contains a JSON message.";
53 response.headers().set_content_type(U(
"application/json"));
58 const std::map<utility::string_t, utility::string_t> headers)
63 for (
auto param : headers)
65 request.headers().add(param.first, param.second);
68 return client->request(request).then([=](pplx::task<web::http::http_response> responseTask) {
71 auto response = responseTask.get();
73 CheckResponseContentType(response);
75 return response.extract_json().get();
77 catch (
const std::exception &e)
80 mitkThrow() <<
"Getting response went wrong: " << e.what();
86 const utility::string_t &filePath,
87 const std::map<utility::string_t, utility::string_t> headers)
90 auto fileBuffer = std::make_shared<concurrency::streams::streambuf<uint8_t>>();
93 for (
auto param : headers)
95 request.headers().add(param.first, param.second);
99 return file_buffer::open(filePath, std::ios::out)
100 .then([=](
streambuf outFile) -> pplx::task<http_response> {
101 *fileBuffer = outFile;
102 return client->request(methods::GET);
106 auto status = response.status_code();
115 return response.body().read_to_end(*fileBuffer);
118 .then([=](
size_t) {
return fileBuffer->close(); })
120 .then([=]() {
return web::json::value(); });
125 auto client =
new http_client(uri, m_ClientConfig);
128 if (
nullptr != content)
129 request.set_body(*content);
131 return client->request(request).then([=](pplx::task<http_response> responseTask) {
134 auto response = responseTask.get();
136 CheckResponseContentType(response);
138 return response.extract_json().get();
140 catch (std::exception &e)
143 mitkThrow() <<
"Getting response went wrong";
149 const std::vector<unsigned char> *content,
150 const std::map<utility::string_t, utility::string_t> headers)
152 auto request = InitRequest(headers);
153 request.set_method(methods::POST);
155 if (
nullptr != content)
156 request.set_body(*content);
158 return ExecutePost(uri, request);
162 const web::json::value *content,
163 const std::map<utility::string_t, utility::string_t> headers)
165 auto request = InitRequest(headers);
166 request.set_method(methods::POST);
168 if (
nullptr != content)
169 request.set_body(*content);
171 return ExecutePost(uri, request);
174 http_request mitk::RESTClient::InitRequest(
const std::map<utility::string_t, utility::string_t> headers)
178 for (
auto param : headers)
180 request.headers().add(param.first, param.second);
185 pplx::task<web::json::value> mitk::RESTClient::ExecutePost(
const web::uri &uri,
http_request request)
187 auto client =
new http_client(uri, m_ClientConfig);
188 return client->request(request).then([=](pplx::task<http_response> responseTask) {
191 auto response = responseTask.get();
193 CheckResponseContentType(response);
195 return response.extract_json().get();
197 catch (std::exception &e)
200 mitkThrow() <<
"Getting response went wrong";
web::http::http_request http_request
web::http::methods methods
pplx::task< web::json::value > Put(const web::uri &uri, const web::json::value *content)
Executes a HTTP PUT request with given uri and the content given as json.
concurrency::streams::file_buffer< uint8_t > file_buffer
pplx::task< web::json::value > Get(const web::uri &uri, const std::map< utility::string_t, utility::string_t > headers)
Executes a HTTP GET request with the given uri and returns a task waiting for a json object...
web::http::client::http_client http_client
web::http::http_response http_response
static std::string convertToUtf8(const utility::string_t &string)
Converts the given std::wstring into a std::string representation.
web::http::http_request http_request
web::http::status_codes status_codes
pplx::task< web::json::value > Post(const web::uri &uri, const web::json::value *content, const std::map< utility::string_t, utility::string_t > headers)
Executes a HTTP POST request with given uri and the content given as json.
concurrency::streams::streambuf< uint8_t > streambuf