1

I have succesfully retrieved the data using a java HttpURLConnection and setting request method as "GET". I would like to implement this in c++ as well (I'm using poco). The URL I am trying to access is "https://open.faceit.com/data/v4".

When I send the request I get a status code 400 (Bad Request) and the response message "400 The plain HTTP request was sent to HTTPS port". Why would using HTTP in java work, but using POCO in c++ I get this error? Should I be using HTTPSClientSession instead, and if so why was I able to use HTTP in java?

java code that gives return code 200

URL url = new URL(dataURL);
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setRequestMethod("GET");
hc.setRequestProperty("Authorization", "Bearer " + code);
hc.setDoOutput(true);
int rCode = hc.getResponseCode();

c++ thats giving me return 400

Poco::URI uri("https://open.faceit.com/data/v4/players?nickname=FadesfasT&game=CSGO");
std::string path(uri.getPathAndQuery());
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
request.add("Authorization", "Bearer" + token);
request.setContentType("application/json");
session.sendRequest(request);
HTTPResponse response;
std::istream& in_stream = session.receiveResponse(response);
std::ostringstream out_stream;
Poco::StreamCopier::copyStream(in_stream, out_stream);
std::cout << out_stream.str() << std::endl;
Zach
  • 25
  • 5
  • Since this seems to have to do with ports / uris it would be helpful to us answerers to know what's behind the variables _uri_ and _path_ in your C++ snippet. – nada Aug 12 '19 at 12:38
  • @nada Added the uri and path variables, sorry I didnt have them in there already! – Zach Aug 12 '19 at 13:11
  • It seems you're mixing https uris with http requests, pick one, then use it in the url + request. Java (idk really) might automatically pick the appropriate request based on your uri, while POCO doesn't (other C++ libraries might, it's a library issue, not language issue). – nada Aug 12 '19 at 13:23
  • @nada Thanks, I assume you mean either alter the uri to "http://" or use a HTTPS client instead? I tried altering the uri to just "http://" and i receive a 301 response (HTTP_MOVED_PERMANENTLY). – Zach Aug 12 '19 at 13:45
  • The server seems to redirect http requests to https, which means you have to use https. – nada Aug 12 '19 at 14:40
  • @nada Alright, thanks again. It seems quite a bit more in-depth, but I'll try and figure out the https client when I have time! – Zach Aug 12 '19 at 15:08
  • There are easier to use HTTPS libraries out there like [libhttp](https://github.com/yhirose/cpp-httplib) (check the example, yes it's that easy) or [libcurl](https://curl.haxx.se/libcurl/c/https.html). – nada Aug 12 '19 at 15:10
  • Wow, libcurl worked like a charm! Wish I had come across this before dumping all that time into poco.. I really appreciate all the help, @nada, you are awesome!! If you want to put in an answer I would glady accept it. (still new on here, assuming thats how it works) – Zach Aug 12 '19 at 16:53

1 Answers1

1

The problem with your C++ snippet is that you are mixing an URL containing a HTTPS address with a request targeting HTTP.

My uneducated guess about why the issue does not appear in Java is that it recognizes the 'https' in the address and automatically uses an appropriate handler for that. Fact is, in C++ or at least in POCO you have to pick the appropriate request yourself.

Basically you have 3 options:

  1. Use a HTTP URL with the HTTP request that you have already written.
    This does not really seem to be an option, because your URL redirects HTTP to HTTPS so this would not work.
  2. Use a HTTPS URL and change your request to HTTPS. It might be more difficult than option 1, but not that hard and there exists a SO question discussing this. To summarize:

    Should I be using HTTPSClientSession instead [...] ?

    Yes, if you decide to go with this option

  3. Use a library other than POCO, which seems to be the solution in your case as mentioned in the comments. I for example know of CURL and libhttp, which imo are at least as easy to use as POCO, if not easier. The links lead to pages containing examples on how to execute HTTPS requests.

nada
  • 2,109
  • 2
  • 16
  • 23