I want to create a dll file that can call some apis and return response (this will be used in a Unity project).
To make thing easier I installed cpprestsdk and followed this simple tutorial and it worked perfectly.
But when I tried changing my code so that I can create a dll, I started getting compilation errors.
Below is my source code -
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "pch.h"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
extern "C" {
void SendRequest() {
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("https://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception& e)
{
printf("Error exception:%s\n", e.what());
}
}
}
and my header file -
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#define SENDREQUEST_API __declspec(dllexport)
extern "C" {
SENDREQUEST_API void SendRequest();
}
and the errors I am getting (truncated) -
1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(5,24): error C2871: 'utility': a namespace with this name does not exist
1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(6,20): error C2871: 'web': a namespace with this name does not exist
1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(7,17): error C2653: 'web': is not a class or namespace name
How can I resolve above errors?
PS: I am using Visual Studio to create dll(s) and able to successfully create a dll that contains no external dependencies (followed from here)
Thanks
If I put all the code snippets in the header file there is no problem.