Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Web requests  (Read 3593 times)

0 Members and 2 Guests are viewing this topic.

Wylex

  • Newbie
  • *
  • Posts: 5
    • View Profile
Web requests
« on: April 15, 2016, 08:13:19 pm »
Hi,

I've never used a REST API before but I'd like to get introduced to them.

I wanted to learn what they are and how to use them so I've picked up one for testing its functionalities. I found this https://www.themoviedb.org/ movie database that provides some interesting functions. I learned how to use those functions on my browser with urls like "http://api.themoviedb.org/3/discover/movie?api_key=###" and know I'd like to use those methods on a c++ program.

I found the sfml tutorial http://www.sfml-dev.org/tutorials/2.3/network-http.php  that if I'm not wrong explains how to do it, however there's something I'm doing wrong:

I followed the tutorial and wrote this:
#include <iostream>
#include <SFML/Network.hpp>

int main() {
        sf::Http http("http://api.themoviedb.org/3/");

        sf::Http::Request request;
        request.setMethod(sf::Http::Request::Get);
        request.setUri("/movie/popular");

        sf::Http::Response response = http.sendRequest(request);
        std::cout << "status: " << response.getStatus() << std::endl;
        std::cout << "HTTP version: " << response.getMajorHttpVersion() << "." << response.getMinorHttpVersion() << std::endl;
        std::cout << "Content-Type header:" << response.getField("Content-Type") << std::endl;
        std::cout << "body: " << response.getBody() << std::endl;

        return 0;
}
 

The status response is always 1001 witch means "connection failed". I know I need to pass the api_key since its a required parameter but I don't know how. I tried this:
request.setField("WWW-Authenticate", "###");
But I get the same status response.

Could anyone point me to the right direction please? I'm quite lost, it's the first time I try to use this libraries.

Note: I replaced my personal API key with "###"

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Web requests
« Reply #1 on: April 15, 2016, 08:37:55 pm »
Does the API allow you to just set the HTTP field WWW-Authenticate or was that something you just thought of and tried?

Since the URL uses ?api_key=###, you'll provide the API key as GET data in the body.
request.setBody("api_key=###");

Might help if you read up how the HTTP protocol works, because once you get that a REST API becomes pretty obvious. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Wylex

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Web requests
« Reply #2 on: April 15, 2016, 09:00:13 pm »
The WWW-Authenticate was something I read on the web and I thought was worth a try.

Besides that, I read on the tutorial that: "The body of the page (used only with the POST method)" so I assumed I didn't have to use the setBody function (I'm using a Get method... right?) though I tried already anyway but it didn't change anything.

Is it possible there's something wrong elsewhere?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Web requests
« Reply #3 on: April 16, 2016, 12:35:01 am »
I thought it would work with the body, but maybe that's really just for POST data. What you can do is simply add it to the URI. This worked for me:

#include <iostream>
#include <SFML/Network.hpp>

int main() {
    sf::Http http("http://api.themoviedb.org");

    sf::Http::Request request;
    request.setMethod(sf::Http::Request::Get);
    request.setUri("/3/discover/movie?api_key=###");

    sf::Http::Response response = http.sendRequest(request);
    std::cout << "status: " << response.getStatus() << "\n";
    std::cout << "HTTP version: " << response.getMajorHttpVersion() << "." << response.getMinorHttpVersion() << "\n";
    std::cout << "Content-Type header:" << response.getField("Content-Type") << "\n";
    std::cout << "body: " << response.getBody() << std::endl;
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Web requests
« Reply #4 on: April 16, 2016, 12:42:32 am »
1001 is a SFML custom error, not a HTTP error. If something at the level of SFML fails, you have most probably used the library wrong. In your case, the host name (passed to the constructor) must not contain the resource path (passed to setUri()).

WWW-Authenticate is a response header field accompanying a 401 error, indicating what type of authentication is required. To pass the API key, one would rather use basic HTTP authentication (the Authorization header). It looks like your specific API requires a query parameter instead.

When using REST APIs, you should understand how the HTTP protocol works: request/response, typical headers and status codes, authentication schemes...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Wylex

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Web requests
« Reply #5 on: April 16, 2016, 11:37:02 am »
Ok, I think it works now, thanks. I'll take a look at the HTTP protocol as you said Nexus.

 

anything