jgaa / restc-cpp

Modern C++ REST Client library
MIT License
615 stars 95 forks source link

Problem with Coinbase REST Api. #137

Closed jepessen closed 2 years ago

jepessen commented 2 years ago

I'm trying to use the library in order to get data from coinbase API (https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproducts-1).

I'm on Windows 11.

This is my code sample:

#include <iostream>
#ifdef WIN32
#include <SDKDDKVer.h>
#endif
#include "restc-cpp/restc-cpp.h"
#include "restc-cpp/RequestBuilder.h"
#include <memory>

int main(int argc, char* argv[]) {

  using restc_cpp::RestClient;
  using restc_cpp::RequestBuilder;
  using restc_cpp::Context;
  using boost::asio::ssl::context;

  constexpr auto GetAllKnownTradingPairsRequest{ "https://api.exchange.coinbase.com/products" };
  //constexpr auto GetAllKnownTradingPairsRequest{ "https://lastviking.eu/files/api" };
  constexpr auto AcceptHeader{ "Accept" };
  constexpr auto AcceptHeaderValue{ "application/json" };

  auto tls_ctx = std::make_shared<context>(context::tlsv12_client);
  auto client = RestClient::Create(tls_ctx);

  try {
    auto contextFunction = [&](Context& ctx) {
      auto reply = RequestBuilder(ctx)
        .Get(GetAllKnownTradingPairsRequest)
        .Header(AcceptHeader, AcceptHeaderValue)
        .Execute();

      std::cout << "Response Code is: " << reply->GetResponseCode();
    };
    client->ProcessWithPromise(contextFunction).get();
  }
  catch (std::exception& e) {
    std::cout << "Exception is: " << e.what() << std::endl;
  }
  return 0;
}

The code compiles fine, but when I run it I catch the exception and the output is

Exception is: Request failed with HTTP error: 400 Bad Request

If I use https://lastviking.eu/files/api instead of https://api.exchange.coinbase.com/products, everything it's fine and I obtain the correct response code:

Response Code is: 200

I can't understand what I'm doing wrong. According to Coinbase documentation, I'm doing the right thing. Just copy and paste the address into a browser returns the expected json to me.

I've tried to change the boost::asio::ssl::context options but with no luck.

Someone knows what I'm doing wrong?

jgaa commented 2 years ago

Http 400 usually mean that there is something wrong with the request data. What does their api documentation say about this error code?

jepessen commented 2 years ago

The API does not say anything, specific, but the reply should contain in the body a message with more specific details. Is there a way to retrieve the body from a reply when the response code is 400?

jgaa commented 2 years ago

Yes, you can turn off the throwing of exceptions on HTTP errors. That let you process error responses just like other responses.

For an example, see here: https://github.com/jgaa/restc-cpp/blob/b9c2aef049dbdb3de86db68ed15bd797bd8269fc/tests/functional/PropertiesTests.cpp#L26

jepessen commented 2 years ago

Ok thanks. Removing the exception I can see the error message in the body, saying that the "User-Agent" header was missing. Adding it solved the problem. Thanks.