-
I need to remove a remove key from the incoming httpContext.Request.QueryString. I tried using QueryParameterRemoveTransform, but have been unsuccessful, see details below. // Incoming Request --- https://localhost:5001//api/account?param1=NSWSJ9
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.Map("/api/account", async httpContext =>
{
var context = new RequestTransformContext
{
Query = new QueryTransformContext(httpContext.Request)
};
var transform = new QueryParameterRemoveTransform("param1");
await transform.ApplyAsync(context);
await httpProxy.ProxyAsync(httpContext, "https://api.ssi-ns2.net/alerts/active?area=xx2", httpClient, requestOptions, transformer);
var errorFeature = httpContext.Features.Get<IProxyErrorFeature>();
if (errorFeature != null)
{
var error = errorFeature.Error;
var exception = errorFeature.Exception;
}
});
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It looks like you started here and are working with the low level IHttpProxy. QueryParameterRemoveTransform is part of the higher level proxy infrastructure and wasn't designed to work directly with IHttpProxy. When using IHttpProxy you have the transformer that gives you direct access to the proxy request to make changes. private class CustomTransformer : HttpTransformer
{
public override async Task TransformRequestAsync(HttpContext httpContext,
HttpRequestMessage proxyRequest, string destinationPrefix)
{
// Copy headers normally and then remove the original host.
// Use the destination host from proxyRequest.RequestUri instead.
await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix);
proxyRequest.Headers.Host = null;
}
} You can take the QueryParameterRemoveTransform and move it inside CustomTransformer something like this. Note you have to assign the proxy request RequestUri with your changes: private class CustomTransformer : HttpTransformer
{
private readonly QueryParameterRemoveTransform _removeParam = new("param1");
public override async Task TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix)
{
await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix);
var context = new RequestTransformContext
{
Query = new QueryTransformContext(httpContext.Request)
};
await _removeParam.ApplyAsync(context);
proxyRequest.RequestUri = new Uri(context.DestinationPrefix + context.Path + context.Query.QueryString);
}
} Side note: The Note to self: If this works it should be added to the direct proxying doc. |
Beta Was this translation helpful? Give feedback.
It looks like you started here and are working with the low level IHttpProxy.
QueryParameterRemoveTransform is part of the higher level proxy infrastructure and wasn't designed to work directly with IHttpProxy. When using IHttpProxy you have the transformer that gives you direct access to the proxy request to make changes.