Home > OS >  vk api on boost c doesn't work correctly
vk api on boost c doesn't work correctly

Time:02-05

I wrote a some code that should send GET request and get response.
It works for ip-api.com and returns me json file.
But for api.vk.com it returns html as that:

 <html>
    <head><title>301 Moved Permanently</title></head>
    <body>
        <center><h1>301 Moved Permanently</h1></center>
        <hr><center>kittenx</center>
    </body>
</html>

The most interesting thing is that the program returns the correct link, after opening which the desired GET request will be executed.

main.cpp:

#include <iostream>

#include "client.hpp"
#include "json.hpp"

std::string get_token(const std::string &);

int main()
{
    std::string token = get_token("data/token1");    
    

    std::string query = "https://api.vk.com/method/groups.getMembers?access_token="   token   "&v=5.13&group_id=klubauto";


    std::cout << query << "\n\n\n";

    Client client(url);
    client.send_request(query);
    std::string response = client.get_response();


    std::cout << response << std::endl;


    return 0;
}

client.hpp:

#pragma once

#include <string>

#include <boost/beast.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>


namespace http = boost::beast::http;



class Client
{
public:

    Client();
    Client(const std::string &api);
    ~Client();

    void send_request(const std::string &arguments);
    std::string get_response();
    
    
private:
    boost::asio::io_context io;
    boost::asio::ip::tcp::resolver resolver;
    boost::asio::ip::tcp::socket socket;

    std::string url;

};

client.cpp

#include "client.hpp"

/*
*   Constructors
*/
Client::Client() : url("google.com"), resolver(io), socket(io)
{
    boost::asio::connect(socket, resolver.resolve(url, "80"));
}

Client::Client(const std::string &api) : url(api), resolver(io), socket(io)
{
    boost::asio::connect(socket, resolver.resolve(url, "80"));
}

/*
*   Destructor
*/
Client::~Client()
{
    socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
}


/*
*   Send request
*/
void Client::send_request(const std::string &arguments)
{
    http::request<http::string_body> req(http::verb::get, arguments, 11);

    req.set(http::field::host, url);
    req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

    http::write(socket, req);
}


/*
*   Get response
*/
std::string Client::get_response()
{

    std::string response;
    {
        boost::beast::flat_buffer buffer;
        http::response<http::dynamic_body> res;
        http::read(socket, buffer, res);
        response = boost::beast::buffers_to_string(res.body().data());
    }

    return response;
}

I would like to receive a json file in the response variable, please tell me how to achieve this?

CodePudding user response:

Like I commented, that's how HTTP works: Servers can redirect to a better/new location.

I assume the prime reason for this is because your connection is not HTTPS, and that's what the end-points require. So, fix that first.

Next, your query includes the base URL, which is another error.

Live Demo

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <string>
namespace http = boost::beast::http;
namespace ssl  = boost::asio::ssl;
using boost::asio::ip::tcp;

class Client {
  public:
    Client(const std::string& host = "google.com") : _host(host) {
        _ctx.set_default_verify_paths();
        connect(_socket.lowest_layer(),
                tcp::resolver{_io}.resolve(_host, "https"));
        _socket.handshake(ssl::stream_base::client);
    }

    void send_request(const std::string& query)
    {
        http::request<http::string_body> req(http::verb::get, query, 11);
        req.set(http::field::host, _host);
        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
        http::write(_socket, req);
    }

    std::string get_response() {
        http::response<http::string_body> res;
        boost::beast::flat_buffer         buffer;
        http::read(_socket, buffer, res);
        return std::move(res.body());
    }

  private:
    boost::asio::io_context  _io;
    ssl::context             _ctx{ssl::context::sslv23_client};
    ssl::stream<tcp::socket> _socket{_io, _ctx};
    std::string              _host;
};

#include <iostream>
#include <boost/json.hpp>
#include <boost/json/src.hpp> // for COLIRU header-only
namespace json = boost::json;

std::string get_token(const std::string&) { return ""; }

int main()
{
    Client client("api.vk.com");
    client.send_request("/method/groups.getMembers?access_token="  
                        get_token("data/token1")   "&v=5.13&group_id=klubauto");

    std::cout << json::parse(client.get_response()) << std::endl;
}

Coliru doesn't allow public network access:

terminate called after throwing an instance of 'boost::wrapexcept<boost::system::system_error>'
  what():  resolve: Service not found

But on my machine it correctly says:

{"error":{"error_code":5,"error_msg":"User authorization failed: no access_token passed.","request_params":[{"key":"v","value":"5.13"},{"key":"group_id","value":"klubauto"},{"key":"method","value":"groups.getMembers"},{"key":"oauth","value":"1"}]}}

Note I included quite a number of simplifications along the way.

  •  Tags:  
  • Related