Technical Tip: Backend response changes depending on presence of 'X-Requested-With' header when traffic passes through FortiWeb
Description | This article describes a scenario where a web application returns different response content types when accessed through FortiWeb. The backend application may return application/json when the request contains the header X-Requested-With: XMLHttpRequest, but return text/html when the header is absent. This behavior can cause application functions, such as buttons or dynamic content loading, to fail because the frontend expects a JSON response. |
Scope | FortiWeb. |
Solution | In some web applications, backend logic determines whether a request is an AJAX request by checking for the HTTP header: X-Requested-With: XMLHttpRequest. If the header is present, the backend returns a JSON response. If the header is not present, the backend returns a full HTML page instead. When accessing the application through FortiWeb, the browser request may not include this header. As a result, the backend application returns HTML rather than JSON, which may cause parts of the frontend application to stop functioning. The following validation tests demonstrate this behavior.
 ![]() Â
 ![]()  These tests confirm that the backend application changes the response type depending on whether the X-Requested-With header is present. FortiWeb forwards the request as received from the client and does not modify the Content-Type header in this scenario. Possible causes include the following:
 FortiWeb workaround. FortiWeb can insert the header before forwarding the request to the backend using a URL rewrite rule. Example configuration:  The rule can then be added to a URL rewrite policy applied to the relevant web protection profile. This configuration inserts the X-Requested-With: XMLHttpRequest header for matching requests, allowing the backend application to return the expected JSON response. As a best practice, the header rewrite should be scoped only to the relevant API paths to avoid affecting other application requests. |


