C
C
cunion2021-01-27 22:24:21
C++ / C#
cunion, 2021-01-27 22:24:21

Why is the HTTP response body empty?

Hello! Used boost::beast and decided to get the HTML page en.wikipedia.org/wiki/HTTP. But when after sending the request, the response came with an empty body. What is the problem?

#include <iostream>
#include <boost/beast.hpp>
using namespace boost::beast;

int main()
{
  // Подключение к серверу
  net::io_context io;
  net::ip::tcp::socket socket(io);
  net::ip::tcp::resolver resolver(io);

  net::connect(socket, resolver.resolve("ru.wikipedia.org", "80"));


  // Отправка запроса
  http::request<http::empty_body> request(http::verb::get, "/wiki/HTTP", 11);

  request.set(http::field::host, "ru.wikipedia.org");
  request.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

  http::write(socket, request);


  // Чтение ответа
  std::string string_response;
  {
    flat_buffer buffer;
    http::response<http::dynamic_body> response;

    http::read(socket, buffer, response);

    string_response = buffers_to_string(response.body().data());
  }

  // На консоль выводится пустая строка
  std::cout << string_response << std::endl;


  socket.shutdown(net::ip::tcp::socket::shutdown_both);

  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2021-01-27
@0hquazEd

In the response headers, you can search for "HTTP/1.1 301 TLS Redirect"
Wiki will redirect you to https, but you apparently do not support redirect
Try https://en.wikipedia.org right away

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question