curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: How to work with Amadeus ?

From: Gisle Vanem via curl-users <curl-users_at_cool.haxx.se>
Date: Tue, 8 Sep 2020 12:29:10 +0200

Dan Richter wrote:

> I am a newbie to cURL on C++ (Visual Studio 2017).
>
> Any advice how to modify the working sample code below to send Authorization Request/Response (token) to Amadeus API and
> than send the actual Amadeus command ? See https://developers.amadeus.com/self-service/apis-docs/guides/authorization-262
>
> // CurlTest.cpp
>
> #define CURL_STATICLIB
> #include <iostream>
> #include <string>
>
> #include "curl/curl.h"
>
> static size_t my_write(void* buffer, size_t size, size_t nmemb, void* param)
> {
>     std::string& text = *static_cast<std::string*>(param);
>     size_t totalsize = size * nmemb;
>     text.append(static_cast<char*>(buffer), totalsize);
>     return totalsize;
> }
>
> int main()
> {
>     /*std::cout << "Hello World!\n";*/
>     std::string result;
>     CURL* curl;
>     CURLcode res;
>
>     curl = curl_easy_init();
>     if (curl) {
>         curl_easy_setopt(curl, CURLOPT_URL, "https://tcno.co/hello.txt");
>         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
>         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
>
>         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
>
>         res = curl_easy_perform(curl);
>
>         curl_easy_cleanup(curl);
>
>         if (CURLE_OK != res) {
>             std::cerr << "CURL error: " << res << '\n';
>         }
>     }
>     curl_global_cleanup();
>
>     std::cout << result << "\n\n";
> }
>

Using the '--libcurl -' option is handy to see what would
be done.

Put something like this in a 'curl-amadeus.bat':
   set CLIENT_ID=none
   set CLIENT_SECRET=01234567

   curl.exe -X POST --libcurl - %* ^
      -H "Content-Type: application/x-www-form-urlencoded" ^
      -d "grant_type=client_credentials&client_id=%CLIENT_ID%&client_secret=%CLIENT_SECRET%" ^
      --write-out json ^
      https://test.api.amadeus.com/v1/security/oauth2/token

and note what 'curl.exe' spits out:
   CURLcode ret;
   CURL *hnd;
   struct curl_slist *slist1;

   slist1 = NULL;
   slist1 = curl_slist_append(slist1, "Content-Type: application/x-www-form-urlencoded");

   hnd = curl_easy_init();

   ...
   curl_easy_setopt(hnd, CURLOPT_POSTFIELDS,
            "grant_type=client_credentials&client_id=none&client_secret=01234567");
   curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)67);

   curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
   curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
   ...

So add those libcurl calls to your code.

But Amadeus says:
   "title": "Invalid parameters"

-- 
--gv
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2020-09-08