How to treat redirect as result #3136
-
Hello, I'm trying to consume an API for getting an url which is sadly implemented as 303 redirect - is there a way to configure the generator or use the generated code to consume this API without following the redirect and instead just getting the value of Redacted snip from openapi: paths:
/v1/auth/userurl:
get:
operationId: GetUserUrl
summary: "Get URL where the user should be redirected."
parameters:
- name: state
in: query
required: false
description: State to be passed onto client.
schema:
type: string
responses:
'303':
headers:
Location:
description: Redirect url
schema:
type: string |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @Kukkimonsuta |
Beta Was this translation helpful? Give feedback.
-
Thank you for quick reaction! Came up with the following - looks bit clunky, but it will do string? location = null;
await client.User.GetAsync(request =>
{
request.Options.Add(new RedirectHandlerOption()
{
ShouldRedirect = (_) => false,
});
request.Options.Add(new ResponseHandlerOption()
{
ResponseHandler = new CatchRedirectResponseHandler(l => location = l),
});
});
Console.WriteLine($"Received location: {location}");
public class CatchRedirectResponseHandler : IResponseHandler
{
public Action<string> Handler { get; }
public CatchRedirectResponseHandler(Action<string> handler)
{
Handler = handler ?? throw new ArgumentNullException(nameof(handler));
}
public Task<ModelType?> HandleResponseAsync<NativeResponseType, ModelType>(NativeResponseType response, Dictionary<string, ParsableFactory<IParsable>>? errorMappings)
{
if (response is HttpResponseMessage { StatusCode: >= HttpStatusCode.MultipleChoices and <= (HttpStatusCode)399 } httpResponseMessage)
{
if (httpResponseMessage.Headers.TryGetValues("Location", out var locations) && locations.Any())
{
Handler(locations.First());
}
}
return Task.FromResult<ModelType?>(default);
}
} |
Beta Was this translation helpful? Give feedback.
Hi @Kukkimonsuta
You might be able to implement that using a response handler option (pass it to the request as you make it)