public void HandleResponse(IInterceptedRequest request, IInterceptedResponse response) { _logger.Information("Intercepting response {RequestId}, Status code: {StatusCode}", request.RequestId, response.StatusCode); // modify statuscode response.StatusCode++; }
public HttpResponseMessage OnResponseFailed(IInterceptedRequest request) { return(new HttpResponseMessage(HttpStatusCode.GatewayTimeout) { Content = new StringContent(GetBody()) }); }
public HttpResponseMessage OnResponseReceived(IInterceptedRequest request, IInterceptedResponse response) { // Test modify the status code if (request.Url.EndsWith("WhatIsTheAnswerToLiveTheUniverseAndEverything")) { response.StatusCode = HttpStatusCode.ExpectationFailed; } // Test modify the response if (request.HttpHeaders.TryGetValue("Content-Type", out var contentType) && contentType == "application/json") { using (var reader = new StreamReader(response.Content)) { // get original content var content = reader.ReadToEnd(); _logger.Information("Received content {Content}", content); // modify content content = $"{{ \"modified\": true, \"receivedContent\": {content} }}"; response.Content = new MemoryStream(Encoding.UTF8.GetBytes(content)); } } return(null); }
public void HandleRequest(IInterceptedRequest request) { _logger.Information("Intercepting request {RequestId} HTTP {Method} {Url}", request.RequestId, request.HttpMethod, request.Url); if (request.HttpMethod == "POST" && request.Stream.Length > 0) { using (var reader = new StreamReader(request.Stream)) { // get original content var content = reader.ReadToEnd(); _logger.Information("Received content {Content}", content); // modify content content = $"{{ \"modified\": true, \"receivedContent\": {content} }}"; request.Stream = new MemoryStream(Encoding.UTF8.GetBytes(content)) { Position = 0 }; // Correct Content-Length header var headers = request.CloneHttpHeaders(); if (headers.ContainsKey("Content-Length")) { headers["Content-Length"] = request.Stream.Length.ToString(); } request.HttpHeaders = headers; } } }
public HttpResponseMessage OnResponseReceived(IInterceptedRequest request, IInterceptedResponse response) { if (request.Url.EndsWith("WhatIsTheAnswerToLiveTheUniverseAndEverything")) { response.StatusCode = HttpStatusCode.ExpectationFailed; } return(null); }
/// <summary> /// This method can modify the request and prevent further processing by returning an <see cref="HttpResponseMessage"/>. /// </summary> /// <param name="request">The request that can be modified.</param> /// <returns>If the returned <see cref="HttpResponseMessage"/> is not null then it will immediately be send out to the client without any further processing.</returns> public HttpResponseMessage OnRequestReceived(IInterceptedRequest request) { _logger?.Debug($"{nameof(DemoRequestInterceptor)}.{nameof(OnRequestReceived)} is called."); // Example: Set Request expiration if (request.HttpHeaders.TryGetValue("Request-Expiration", out var expirationValue) && TimeSpan.TryParse(expirationValue, out var expiration)) { _logger?.Information("Interceptor is setting RabbitMQ TTL to {RequestExpiration} for Request {RequestId}", expiration, request.RequestId); request.Expiration = expiration; return(null); } // Example: Set AcknowledgmentMode if (request.HttpHeaders.TryGetValue("Acknowledgment-Mode", out var ackModeValue) && Enum.TryParse(ackModeValue, out AcknowledgmentMode ackMode)) { // This request will be auto-acknowledged when taken out of RabbitMQ. // When there is a problem with SignalR or the OnPremiseConnector, this request may not reach the remote API. request.AcknowledgmentMode = ackMode; return(null); } // If a PUT is received, we immediately reject and tell the user to use PATCH instead if (String.Equals(request.HttpMethod, HttpMethod.Put.Method, StringComparison.InvariantCultureIgnoreCase)) { return(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed) { ReasonPhrase = "Use PATCH instead of PUT" }); } // If a PATCH is received, we internally change that to PUT if (String.Equals(request.HttpMethod, "PATCH", StringComparison.InvariantCultureIgnoreCase)) { request.HttpMethod = HttpMethod.Put.Method; } // Append something to the url request.Url += (request.Url.Contains("?") ? "&" : "?") + "parameterAddedFromDemoInterceptor=valueAddedFromDemoInterceptor"; // Add an HTTP header var headers = request.CloneHttpHeaders(); headers.Add("X-ThinkectureRelay-Example", $"Added by {nameof(DemoRequestInterceptor)}"); request.HttpHeaders = headers; _logger?.Information("Demo interceptor modified request: {@Request}", request); return(null); }
/// <summary> /// This method can modify the request and prevent further processing by returning an <see cref="HttpResponseMessage"/>. /// </summary> /// <param name="request">The request that can be modified.</param> /// <returns>If the returned <see cref="HttpResponseMessage"/> is not null then it will immediately be send out to the client without any further processing.</returns> public HttpResponseMessage OnRequestReceived(IInterceptedRequest request) { _logger?.Debug($"{nameof(DemoRequestInterceptor)}.{nameof(OnRequestReceived)} is called."); if (_context.IsLocal) { _logger?.Debug("This request comes from localhost."); } // Example: Move query parameters into own JSON property if (request.Url.Contains("?")) { var parts = request.Url.Split('?'); request.Url = parts[0]; request.Properties = new Dictionary <string, object>() { { "Parameter", parts[1] } }; } // Example: Modify content if (request.HttpHeaders.TryGetValue("Content-Type", out var contentType) && contentType == "application/json") { using (var reader = new StreamReader(request.Content)) { // get original content var content = reader.ReadToEnd(); _logger?.Information("Received content {Content}", content); // modify content content = $"{{ \"modified\": true, \"originalContent\": {content} }}"; request.Content = new MemoryStream(Encoding.UTF8.GetBytes(content)); } } // Example: Set Request expiration if (request.HttpHeaders.TryGetValue("Request-Expiration", out var expirationValue) && TimeSpan.TryParse(expirationValue, out var expiration)) { _logger?.Information("Interceptor is setting RabbitMQ TTL to {RequestExpiration} for Request {RequestId}", expiration, request.RequestId); request.Expiration = expiration; return(null); } // Example: Set AcknowledgmentMode if (request.HttpHeaders.TryGetValue("Acknowledgment-Mode", out var ackModeValue) && Enum.TryParse(ackModeValue, out AcknowledgmentMode ackMode)) { // This request will be auto-acknowledged when taken out of RabbitMQ. // When there is a problem with SignalR or the OnPremiseConnector, this request may not reach the remote API. request.AcknowledgmentMode = ackMode; return(null); } // If a PUT is received, we immediately reject and tell the user to use PATCH instead if (String.Equals(request.HttpMethod, HttpMethod.Put.Method, StringComparison.InvariantCultureIgnoreCase)) { return(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed) { ReasonPhrase = "Use PATCH instead of PUT" }); } // If a PATCH is received, we internally change that to PUT if (String.Equals(request.HttpMethod, "PATCH", StringComparison.InvariantCultureIgnoreCase)) { request.HttpMethod = HttpMethod.Put.Method; } // Append something to the url request.Url += (request.Url.Contains("?") ? "&" : "?") + "parameterAddedFromDemoInterceptor=valueAddedFromDemoInterceptor"; // Add an HTTP header var headers = request.CloneHttpHeaders(); headers.Add("X-ThinkectureRelay-Example", $"Added by {nameof(DemoRequestInterceptor)}"); request.HttpHeaders = headers; _logger?.Information("Demo interceptor modified request"); return(null); }