Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.