[Help] How do I send "Azure" style request to an API?
stav121 opened this issue · comments
Good evening,
First of all thank you very much for building this project, it has been a huge help for me so far. I'm using it constantly to get responses from APIs, but I'm currently stuck for hours in the same point so I decided to make this post in seek for an answer.
I'm trying to send an API Search Request on Azure that should contain this information: https://i.imgur.com/wMmAODW.png
I'm having problem approaching this using this library, can anybody possibly give me a bit of help on this?
Thanks is advance :)
Hi @unix121
I am glad you found curlcpp useful! I think that you should perform a GET request including an header named "Ocp-Apim-Subscription-Key" with the relative value. Here's an example of how I think you should do, based on that screenshot:
#include "curl_easy.h"
#include "curl_form.h"
#include "curl_header.h"
using curl::curl_header;
using curl::curl_easy;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
/*
* This example shows how to add custom headers to a simple
* curl request.
*/
int main() {
// Let's create an object which will contain a list of headers.
curl_header header;
// Easy object to handle the connection.
curl_easy easy;
// Add custom headers, in this case, your key.
header.add("Ocp-Apim-Subscription-Key:value_of_sub_key");
// Add the headers to the easy object.
easy.add<CURLOPT_HTTPHEADER>(header.get());
easy.add<CURLOPT_URL>("https://api.cognitive.microsoft.com/bing/v5.0/search?q=bill%20gates");
// You can disable verbosity using 0L
easy.add<CURLOPT_VERBOSE>(1L);
try {
// Request execution
easy.perform();
} catch (curl_easy_exception error) {
// If you want to get the entire error stack we can do:
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
}
return 0;
}
Obviously, you have to change "value_of_sub_key" with the one you have. Let me know.
This is exactly what I wanted, it seems like I was constructing the header in a wrong way but you provided the best answer. Thank you very much kind sir!