Can I get the text from the HTTP request?

Asked

Viewed 215 times

0

HTTP/HTTPS requests are sent as text with the format:

inserir a descrição da imagem aqui

In order:

  1. The initial line with the HTTP method and the version
  2. The headers in the format chave: valor
  3. A blank line
  4. The body of the request

Is it possible to get the complete string of this request? It is saved in some file .txt in the directory /tmp?

  • 1

    It would be important to contextualize the problem you are trying to solve. Possible is, because thousands of tools implement this, now what is your need in this specific case?

  • @Julianonunes does not have a specific context is more a curiosity than a problem

  • Then let’s do it. The request itself is not saved in text file, what happens is that you get via some network Sniffer or even through your browser view the traffic of your machine and get this information. Making the request via Postman is also possible. If you are on a web server for example, you can track all the requests that are being made. Anyway, there are several ways to do it, but since your question is very open it is difficult to know which direction to point you.

  • @Julianonunes I’m not sure if you understand, the idea is that my PHP backend can recover this string, not the browser or third-party software (except libs/frameworks PHP)

1 answer

0

You can recover all this information in PHP. It is not available in a file as stated by "please delete my account". In the image, you have a request via POST, the headers and in the body all the information sent.

In the requisition...

To recover the type of request along with the headers you can do this:

$string = $_SERVER['REQUEST_METHOD']."\n"; // pega o tipo de requisição

# pega e armazena todos os cabeçalhos
foreach(getallheaders() as $nome => $valor)
$string .= $nome." : ".$valor."\n";

It will look something like what’s in your image.

To catch the body you can use the $_REQUEST to get everything($_GET, $_POST, $_COOKIE) that was sent. Would look like this:

$string = $_SERVER['REQUEST_METHOD']."\n";
foreach(getallheaders() as $nome => $valor)
    $string .= $nome." : ".$valor."\n";

$string .= "Body:\n";
foreach($_REQUEST as $key => $value)
    $string .= $key." : ".$value."\n";

In response...

In the answer, you can urilize the http_response_code(), that when it is not entered in the parameter with the response type, it returns the current server response.

And to get all the headers sent by the server, you can use the get_headers() that even sends the response status.

The body of the answer is with you... It can be an html, xml, a "hello world!" anything displayed, or nothing. And you can recover in various ways as with the file_get_contents() if this response has a status 200, that is, if there are no errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.