beast icon indicating copy to clipboard operation
beast copied to clipboard

Send Real request to server

Open ghost opened this issue 3 years ago • 25 comments

Hello, i am new in Boost:beast i want sent a real request: ((node(51.249,7.148,51.251,7.152); <;);out meta;) to :https://lz4.overpass-api.de/api/interpreter. and get reponse of this request.any idea please.

ghost avatar May 24 '21 20:05 ghost

Do any of the examples do something close to what you want?

madmongo1 avatar May 24 '21 20:05 madmongo1

I want to sent this " ((node(51.249,7.148,51.251,7.152); <;);out meta;)" to the host that i do in the code in get the reponse i don t now how

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

namespace beast = boost::beast;     // from <boost/beast.hpp>
namespace http = beast::http;       // from <boost/beast/http.hpp>
namespace net = boost::asio;        // from <boost/asio.hpp>
using tcp = net::ip::tcp;           // from <boost/asio/ip/tcp.hpp>

// Performs an HTTP GET and prints the response
int main(int argc, char** argv)
{
    try
    {
        // Check command line arguments.
        if(argc != 4 && argc != 5)
        {
            std::cerr <<
                "Usage: http-client-sync <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
                "Example:\n" <<
                "    http-client-sync www.example.com 80 /\n" <<
                "    http-client-sync www.example.com 80 / 1.0\n";
            return EXIT_FAILURE;
        }
        auto const host = https://lz4.overpass-api.de/api/interpreter;
        auto const port = 443;
        auto const target = argv[3];
        int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;

        // The io_context is required for all I/O
        net::io_context ioc;

        // These objects perform our I/O
        tcp::resolver resolver(ioc);
        beast::tcp_stream stream(ioc);

        // Look up the domain name
        auto const results = resolver.resolve(host, port);

        // Make the connection on the IP address we get from a lookup
        stream.connect(results);

        // Set up an HTTP GET request message
        http::request<http::string_body> req{http::verb::get, target, version};
        req.set(http::field::host, host);
        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

        // Send the HTTP request to the remote host
        http::write(stream, req);

        // This buffer is used for reading and must be persisted
        beast::flat_buffer buffer;

        // Declare a container to hold the response
        http::response<http::dynamic_body> res;

        // Receive the HTTP response
        http::read(stream, buffer, res);

        // Write the message to standard out
        std::cout << res << std::endl;

        // Gracefully close the socket
        beast::error_code ec;
        stream.socket().shutdown(tcp::socket::shutdown_both, ec);

        // not_connected happens sometimes
        // so don't bother reporting it.
        //
        if(ec && ec != beast::errc::not_connected)
            throw beast::system_error{ec};

        // If we get here then the connection is closed gracefully
    }
    catch(std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

ghost avatar May 24 '21 20:05 ghost

change this line:

    http::request<http::string_body> req{http::verb::get, target, version};

to this

    http::request<http::string_body> req{http::verb::post, target, version};

and after this line:

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

add this:

    req.body("((node(51.249,7.148,51.251,7.152); <;);out meta;)");

madmongo1 avatar May 24 '21 21:05 madmongo1

Thanks i will test that

ghost avatar May 24 '21 21:05 ghost

I go this error in this linereq.body("((node(51.249,7.148,51.251,7.152); <;);out meta;)"); the compiler don t inderstand the request.

C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C3861: 'node': identifier not found 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2143: syntax error: missing ')' before ';' 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2059: syntax error: '<' 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2059: syntax error: ')' 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2065: 'out': undeclared identifier 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2146: syntax error: missing ';' before identifier 'meta' 1>C:\Sources\modele\Interfaces\ClientREST\Coeur\CRT_Requete.cpp(24): error C2065: 'meta': undeclared identifier 1> 0 Warning(s) 1> 7 Error(s)

ghost avatar May 24 '21 22:05 ghost

Are you new to C++ ?

vinniefalco avatar May 24 '21 23:05 vinniefalco

yes why

ghost avatar May 25 '21 06:05 ghost

Looks like you've missed out a quote somewhere. You should have a reasonably good understanding of C++ before using Asio and Beast.

madmongo1 avatar May 25 '21 12:05 madmongo1

No the query inside the req.body is a overpass query languagereq.body((node(51.249,7.148,51.251,7.152); <;);out meta;); for that the compiler generate error. the question have you any idea howa to use overpass query language in c++

ghost avatar May 25 '21 13:05 ghost

You need to put the "overpass query language statement in quotes don't you? I assume you're sending it to the web server for processing?

req.body("(node(51.249,7.148,51.251,7.152); <;);out meta;");

madmongo1 avatar May 25 '21 13:05 madmongo1

Yes i want to sent that query to server that i montioned in the host, it s a server Overpass API. I tried in quotes but got that error: error C2661: 'boost::beast::http::message<true,boost::beast::http::string_body,boost::beast::http::fields>::body': no overloaded function takes 1 arguments

ghost avatar May 25 '21 13:05 ghost

My mistake:

https://www.boost.org/doc/libs/1_76_0/libs/beast/doc/html/beast/ref/boost__beast__http__message/body/overload1.html

It should be:

req.body() = "((node(51.249,7.148,51.251,7.152); <;);out meta;)";

madmongo1 avatar May 25 '21 17:05 madmongo1

Thanks ,but now i got this error error: C++ exception with description "end of stream" thrown in the test body.

ghost avatar May 25 '21 20:05 ghost

Great, so the program compiled and is working. The server has closed the stream (the reason for which I have no chance of discerning in an issue tracker) and the resulting uncaught exception has caused the program to terminate, as per the c++ standard.

So far, all correct.

is there a specific Beast-related query I can help with?

madmongo1 avatar May 25 '21 20:05 madmongo1

yes, the programe is compiled now but the probleme is here

  req.set(http::field::host, flux.host);
  req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
  req.body()= "(node(51.249, 7.148, 51.251, 7.152);< ;);out meta;";
  beast::error_code ec;
  http::write(flux.stream, req, ec);

ghost avatar May 25 '21 21:05 ghost

  • To be honest, that message looks as if it's been produced by a google test suite.
  • What makes you think the problem is "here"?

madmongo1 avatar May 26 '21 05:05 madmongo1

Why do you think the exception is thrown by the write? It seems to me more likely that it was thrown by the read, in response to the server closing the connection instead of sending a response.

madmongo1 avatar May 26 '21 06:05 madmongo1

it was probléme in number of port, i make a wrong number.but i got error message in the response

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en"/>
  <title>OSM3S Response</title>
</head>
<body>

<p>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</p>
<p><strong style="color:#FF0000">Error</strong>: encoding error: Your input contains only whitespace. </p>

</body>
</html>

0

ghost avatar May 26 '21 06:05 ghost

OK, Boost.Asio and Boost.Beast are doing what they are supposed to be doing. I have no idea about how your web server should behave. You will need to look up the error message "encoding error: Your input contains only whitespace" on the openstreetmap documentation and/or consult their help/community.

madmongo1 avatar May 26 '21 07:05 madmongo1

I think the probleme is in buffer size the didin t support the size of data response

ghost avatar May 26 '21 09:05 ghost

Beast defaults the response buffer size limit to 8MB. You can increase it with a call to:

std::size_t required_bytes = 64 * 1024 * 1024;  // 64 MB
response.body_limit(required_bytes);

or you can specify unlimited with:

response.body_limit(boost::none);

Never do this on a server, but on a client where you trust the server, it's acceptable.

madmongo1 avatar May 26 '21 10:05 madmongo1

The probleme was not in server overpass api, i just ad the req.prepare_payload(); after req.body()= "(node(51.249, 7.148, 51.251, 7.152);< ;);out meta;"; and i got the same reponse that i got in overpassturbo.

ghost avatar May 27 '21 06:05 ghost

great news!

madmongo1 avatar May 28 '21 02:05 madmongo1

There is a methode to do our own message of exception ?

ghost avatar Jun 02 '21 14:06 ghost

This issue has been open for a while with no activity, has it been resolved?

stale[bot] avatar Jan 09 '22 04:01 stale[bot]