Search code examples
apihttpxmlhttprequesthttp-headers

To detect page request and API/XHR call from backend


Have a single entry service which is acting like a facade/proxy service for downstream services. The service will need able to detect if the request is a "page request" or "api/xhr" request to perform error handling (302 redirection or 401).

So far have considered:

  • To use Accept header and detect text/html follow the following reference, can't tell if this is a good indicator to detect a page request
  • To introduce a custom header for all "api/xhr" request
  • To enforce all the "api/xhr" requests to follow a "/api" pattern (troublesome as for certain application the xhr is not a restful api)

Any good suggestions are welcome


Solution

  • Ended up using Option 1

    • Detect page request using Accept header with value "text/html"

    As we do not use ajax for partial view