Difference between UseStatusCodePagesWithRedirects and UseStatusCodePagesWithReExecute - Status Code Pages in Asp.net core - asp.net-core

I am using UseStatusCodePages Middleware to show status code pages on my application but it shows plain text on UI without any other information,
I want to show UI with Status Code Information along with some other helpful information like Customer Support Number with more user-friendly page.
I found out we can use two extension methods to do that which is UseStatusCodePagesWithRedirects and UseStatusCodePagesWithReExecute. Only Difference I found out from Microsoft Docs is,
UseStatusCodePagesWithRedirects : Send 302 to Client.
UseStatusCodePagesWithReExecute : Send Original Status Code and Executes handler for redirect URL.
Is that the only difference?

I think that the main difference is that UseStatusCodePagesWithRedirects is redirecting you to error controller action method while UseStatusCodePagesWithReExecute is just rendering page with out redirecting
Example
Controller actions
[Route("error/404")]
public IActionResult Error404(int code)
{
return View("Error404");
}
[Route("error/{code}")]
public IActionResult Error(int code)
{
return StatusCode(code);
}
Startup Cinfigue
app.UseStatusCodePagesWithRedirects("/error/{0}");
or
app.UseStatusCodePagesWithReExecute("/error/{0}");
Case 1 (404 Error)
Url : https://localhost:5001/notexits_page
1) UseStatusCodePagesWithRedirects
Result:
Url is: https://localhost:5001/error/404
We see Error404 page
2) UseStatusCodePagesWithReExecute
Result:
Url is: https://localhost:5001/notexits_page
We see Error404 page
Case2 (401 Error)
Url : https://localhost:5001/admin/users
1) UseStatusCodePagesWithRedirects
Result:
Url is: https://localhost:5001/error/401
We stack in infinity loop
1) UseStatusCodePagesWithRedirects
Result:
Url is: https://localhost:5001/admin/users
We see default browser error page for 401 error

When Using app.UseStatusCodePagesWithRedirects("/Error/{0}") and invalid request(lets say "/abc/xyz") is raised then;
Status Code 404 is issued, app.UseStatusCodePagesWithRedirects("/Error/{0}") intercepts the request and 302 status code is issued(which means URI of the requested resource has been changed temporarily)
As 302 is issued another get request is issued which results in change of the url from
"/abc/xyz" to "/Error/404".
As the request is redirected to the specific error controller the status code for the request is 200 ok in the browser developer tool.
But When Using app.UseStatusCodePagesWithReExecute("/Error/{0}") and invalid request(lets say "/abc/xyz") is raised then;
app.UseStatusCodePagesWithReExecute("/Error/{0}") middleware intercepts the 404 status code and re-executes the pipeline pointing it to the URL
As the middleware is re executing the pipeline the original URL "/abc/xyz" in the address bar is preserved. It does not change from "/abc/xyz" to "/Error/{0}".
Also the original status Code(404 in this case) is preserved in the developer tool.

Related

Rest assured is giving 502 in return

I have simple get API
https://abc.xyz.co/index.php?route=efg/api/jkl&key=8454jdgdkjf948754&source=android&user_id=44
when i hit in browser I receive 200 OK but when i send request using rest assured it gives 502 in return.
Below is the code I am using
RestAssured.baseURI = "https://abc.xyz.co/";
RequestSpecification request = RestAssured.given();
Response response = request.queryParam("route", "efg/api/jkl").
queryParam("key","8454jdgdkjf948754").
queryParam("source","android").
queryParam("user_id","44").get("/index.php");
System.out.println(response.getStatusCode());
Someone please look into this
Note: URL shared is dummy URL but format is same as per my project
I think the problem is in base url and endpoint url:
"https://abc.xyz.co/"
"/index.php"
which gives together "https://abc.xyz.co//index.php", so one slash character "/" is redundant. Try to remove it either for base url or endpoint url and it should work.

Custom error status code with gqlgen + go gin

Recently I have been updating my GO REST APIs into graphQl API's and I came across issue where I am unable to customise my status code with gqlgen.
Response I got
Headers
Status Code: 200 OK
{
data: null,
errors: [
{message: "Unauthorized access", path: ["..."]}
]
}
Expected Header
Status Code: 401 UNAUTHORISED
Any help would be really appreciating!
Assume you have a gqlgen resolver similar to this:
func (r *queryResolver) SecretItems(ctx context.Context, userID string,
password string) ([]SecretItems, error) {
// ...
if !isAuthorized(userID, password) {
return nil, errors.New("Unauthorized access")
}
// ...
}
then the described behavior is expected. Errors should be returned as part of
the response body.
GraphQL is transport agnostic. While it is often served over HTTP, it might be
served over other client-server Protocols as well. Handling errors in the
response body requires no assumptions about the protocol. Hence, you shouldn't
rely on HTTP status codes.
Handling errors in the response body has another advantage: Assume a request
contains multiple queries. Some of them succeed, some of them fail. Then the
response can contain the result of successful queries under data and errors
related to failed queries under errors.
References:
GraphQL website
Specification: Response
Hasura: GraphQL vs REST
Possible reason why you expected a 401 status code
The gqlgen docs on
authentication contain an example
where 401 status code is returned.
Why? This happens in a http handler used as middleware on the chi http server.
The 401 status code is not returned by a GraphQL resolver.

Getting the main url on which error occured in Yii 1

We have implemented an error handler for Yii 1. Also we have implemented the mail functionality with this as any error occurred an email will be send to us but the problem is we are not getting the current URL on which error is generating. Like one page controller/action can contain many images favicons etc. So if any image is missing then we are getting the image URL which showing 404 from:
$url = Yii::app()->createAbsoluteUrl(Yii::app()->request->url);
But we are not getting current URL not even in $error = Yii::app()->errorHandler->error.
So we are not getting the page in which image is absent. Please let me know if is there any way to get current page URL as I have tried many ways but all they are returning the missing images URL instead of main page URL for which images are missing.
createAbsoluteUrl() expects route as first argument - it may return random results if you provide URL instead of route (like in your code snippet).
If you want absolute URL of current request, you may use combination of getUrl() and getHostInfo():
$url = Yii::app()->request->getHostInfo() . Yii::app()->request->getUrl();
In case of error you can get current page url using Yii::app()->request->requestUri in Yii 1.

Akka http redirect but change method from POST to GET

I have endpoint using POST method ( registering token for example ), after job done I want to redirect client to "/" url, but using GET not POST.
How to achieve this ?
For now I have:
redirect("/", StatusCodes.PermanentRedirect)
What you want in this case is not a redirect with status code PermanentRedirect, but rather SeeOther (303). So you would have instead: redirect("/", StatusCodes.SeeOther)
You can check in RFC 7231, section 6.4.4: 303 See Other that this guarantees the behavior you describe.

API: Custom 404 not found error response structure

Considering this url: http://example.com/users/1/post/2/likes.
How can I correctly define structure of my 404 response, showing which item is not found? user, post or likes?
You can send an HTTP status code using the header() function, by starting the header with the status code number, followed by the message to send to the user.
header("404 " + $message);
The code that processes the parameters can determine which item isn't found, and put that into $message.