-
Notifications
You must be signed in to change notification settings - Fork 0
/
302 redirect.cs
35 lines (33 loc) · 1.2 KB
/
302 redirect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.Found)
{
if (response.Headers.Location != null)
{
var redirectUrl = response.Headers.Location.ToString();
var redirectRequest = new HttpRequestMessage(HttpMethod.Get, redirectUrl);
response = await this.Context.SendAsync(redirectRequest, this.CancellationToken).ConfigureAwait(true);
}
else
{
// If the Location header is not present, return an error response
response = new HttpResponseMessage(HttpStatusCode.BadRequest);
response.Content = CreateJsonContent("Location header missing in the redirect response.");
}
}
else if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var result = JObject.Parse(responseString);
var newResult = new JObject
{
["wrapped"] = result,
};
response.Content = CreateJsonContent(newResult.ToString());
}
return response;
}
}