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.
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
Asked: 2022-02-26 11:00:00 +0000
Seen: 11 times
Last updated: Mar 16 '22
What steps can be taken to enable http 1.1 persistent connections on the web server?
What are the steps to use .htaccess rules for redirecting all HTTP requests to HTTPS?
How can Angular fix the issue whereby the HTTP get request loop produces jumbled data?
What is the process to restrict Google API key using HTTP referrers?
What is a legitimate web address to obtain the Imagenet dataset?
How can I send a pubsub message when a cloud function with http trigger is activated by someone?
How can I do a simple HTTP get request with Telnet?
How to set custom HTTP headers with SDWebImage?
What is the method for reading a HTTP network request via a socket using C#?