Open Closed

How to get a JSON response on 401 / Unauthorized #5983


User avatar
0
BassaSolutions created
  • ABP Framework version: v6.0.3
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

When sending X-Requested-With :XMLHttpRequest in the header, the correct status is responded when unauthorized: 401 instead of 200.

But the content is still the HTML page:

...
<div class="col">
					<div class="status-content">
						<h1>401</h1>
						<h2 class="text-danger mb-0">
							Unauthorized<small class="text-muted">An internal error occurred during your request!</small>
						</h2>
						<p class="mt-3 mb-4"></p>
						<a href="/" class="btn btn-primary">Go to the homepage</a>
						<a href="javascript:history.back()"
							class="mt-4 mb-1 d-block"><i class="fa fa-long-arrow-left"></i> Go back</a>
					</div>
				</div>
...

How can I get a the default error JSON response content?

{
  "error": {
    "message": "Some error message"
  }
}

I tried to send additional headers: Content-Type: application/json Accept: application/json

But the content is always HTML.

Thank you for your help!


3 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello BassaSolutions,

    could you please provide steps to reproduce.

    Regards,

  • User Avatar
    0
    BassaSolutions created
    1. Load startup template
    2. Call any authorized endpoint with postman (without authorization). Add header: X-Requested-With :XMLHttpRequest
    3. Response content is HTML (target: JSON)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello BassaSolutions,

    Please check this similar issue https://support.abp.io/QA/Questions/3281/Swagger-Authorisation--401-Response-instead-of-login-page-redirect#

    Regards,

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09