How to filter Request Response before reaching to Web Api controller - asp.net-web-api

Hi I am working with Web Api 2, is their any way I can handle request and response before reaching to the API controller.

You may be looking for a DelegatingHandler. These are HTTP Message Handlers that can process the request before it reaches the Controller and can also process the response on the way out of the pipeline. Delegating Handlers can also return the response themselves without calling the rest of the pipeline. You can read about Delegating Handlers here.

Related

I have XCUI test cases in which some API calls are made. How can I get which API calls are being made and get request and response printed in console?

I have to find which API calls are made in the application. I am running the XCUI tests and to mock the API request and response I need to know which APIs are being called in that page. How do I know which API calls are made and how to get the request and response of the same printed in the debug console?
I have tried po self.actionContext.searchEndPoint which gives me endpoint. But I need to get the request and response.

How to design a spring boot application that can process 1k requests a second

I have a rest controller which accepts post requests and returns the statuses of whether the operations are successfull or not. It works fine for 100 requests per second as I have multiple operations underlying it which at the end send the response.
There could be hundreds of users trying to send the requests to the controller, thereby its all done using a completable future and http Async invoker. The problem happens when there are a 1000 requests per second and then the controller threads are exhausted as there are already multiple thread processing multiple requests and all are waiting for there future to be complete and then sending the response.
How can I make my rest controller be able to handle 1000 requests per Second without breaking.
there are already multiple thread processing multiple requests and all are waiting for there future to be complete and then sending the response.
You can actually make you controllers asynchronous by making them return a CompletableFuture. Just chain the calls on the CompletableFuture returned by your service to convert them into a an appropriate response instead of using get() or join():
#RequestMapping
public CompletableFuture<ResponseEntity<…>> processRequest() {
return myService.getStatusFuture()
.thenApply(status -> convertToResponseEntity(status));
}
Of course for this to work properly you should have a truly asynchronous service. If you are using #Async or submitting tasks with CompletableFuture.supplyAsync(), this will just move the problem from the HTTP threadpool to another threadpool.
It depends on the servlet server you are using. In the application.properties file, you can use the server.* properties to set the parameters you need.
In this link you can find these properties under the EMBEDDED SERVER CONFIGURATION section. If you are using the default tomcat embedded server, check out the server.tomcat.* properties. Especially the server.tomcat.accept-count, server.tomcat.max-connections and server.tomcat.max-threads properties.

Spring MVC REST API: Invoke controller method programmatically given URL and JSON request body

I have a general REST API (developed using Spring MVC) that takes a list of API requests as its request body.
Each API request in the list has its own URL and request body.
In the implementation of this general REST API, I need to call the corresponding Spring controller method (in the same app) for each of these individual API requests (with their appropriate URL and request body).
(I will then merge all those individual API responses and return it in one big response from the general REST API).
I've been searching around, but I'm unclear how to programmatically call Spring to execute each individual API request. I would ideally like to get back the ResponseEntity from each call instead of the actual JSON response.
(More information:
On the same app server as the general API, I need to translate the URL and JSON request body for each individual API into the arguments to the controller method. I also need to take the URL and have Spring determine which controller method to invoke itself.)
Any help would be greatly appreciated.
Thanks,
Matt
Answer depend on whether the individual URLs that you are planning to invoke is with in the same server (Accessible without using network call) or not
If it is with in the same app server, spawn multiple threads and invoke the individual methods and join the response together and send it back
If it is not within the same app server, there are many Async Restclients are there besides spring's own webclient/restTemplate etc

Handling Callback URLs in JMeter

My REST Service takes in a JMeter HTTP request and one of the parameters in the request is a callback url.
The REST Service uses this callback url to post back a response.
Is there any JMeter Listener I can use to receive the callback i.e. so that the REST Service can send the response back to a JMeter Listener.
And if possible the Listener can then send in another HTTP Request based on the response.
I'm not sure about your question. But I think you want to receive your REST response and make another request to URL in the response...
By that way, you need to "catch" the response by using Regular Expression Extractor, store URL in the response to a JMeter Variable. Then, you create HTTP Request, and put URL in that JMeter Variable into request.

Access HttpRequestMessage outside Controller Context

Is it possible to access the current request information (HttpRequestMessage) outside of the context of a controller?
I would like to setup a tenant strategy (ITenantIdentificationStrategy) that involves the incoming request headers. In self hosted mode HttpContext.Current is not an option and I haven't found any other solutions.
It sounds like a HttpMessageHandler is what you are looking for. MessageHandlers allow you to perform some kind of processing on every request and provides full access to the request and response message.

Resources