Ask Your Question
1

What is the procedure for identifying an ajax request with boost::beast?

asked 2022-02-26 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-03-16 10:00:00 +0000

bukephalos gravatar image

To identify an AJAX request with Boost::Beast, you can check for the X-Requested-With header in the HTTP request headers. AJAX requests usually include this header with a value of "XMLHttpRequest".

Here's an example code snippet:

http::request<http::string_body> req = ...; // your HTTP request object

auto it = req.find(http::field::sec_websocket_key);
if (it != req.end()) {
  // this is a WebSocket handshake request
} else {
  // check for AJAX request
  it = req.find(http::field::user_agent);
  if (it != req.end() && boost::icontains(it->value(), "XMLHttpRequest")) {
    // this is an AJAX request
  } else {
    // normal HTTP request
  }
}

In this example, we first check for the Sec-WebSocket-Key header to identify a WebSocket handshake request. If that header is not present, we then look for the User-Agent header and see if it contains the string "XMLHttpRequest" to identify an AJAX request. If neither of those headers are present, we assume it's a normal HTTP request.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-26 11:00:00 +0000

Seen: 11 times

Last updated: Mar 16 '22