How do I forward a POST request to a third party server using Spring? - spring

I’m using Spring version 4.1.5.RELEASE and Spring Security 3.2.5.RELEASE. How do I forward a POST request to a third party server when all I have available to me are the HttpServletRequest request and HttpServletResponse response objects?
Note I don’t want to do a redirect because that causes the POST parameters to get lost and I don’t want to append the POST params to the query string, thus changing my request from a POST to a GET.

One of my past projects involved something like a proxy server/middleware, and we had this requirement too.
This straightforward communication involved basically a copy and paste of the original request parameters and headers, and a subsequent POST Request to the final target.

Related

Spring RestTemplate Response string is shorter than expected

I am trying to get an access token via RestTemplate.postForEntity().
myRestTemplate.postForEntity(authBaseUrl, request, Object.class);
I have a specific class for it, but let's use now a simple Object as type. It contains an access_token field.
It works, because I can get response, but the length if the access tokens (which is a string)
is 1196 character long. And I can get the same length in Postman too.
But if I use the intelliJ built-in REST client, the length is 1199.
Only the token from the intelliJ rest client works (So the longer).
Because I always get a new access token, it is impossible to get the same token twice.
How can I debug it?
What could be the problem?
Is the code that generates the response available to you? if so in your response add a header content-length so you can see what the server sent and what you received. Also, debug the server side and see what is being generated. In addition take another 3d party Http client and test it with this client see if you see a difference. The Http clients that you can try are Apache Http client, OK Http client, or my favorite - a very simplistic client written by me as part of my own Open Source MgntUtils library. Here is the Javadoc for my http client Here is a link to a similar question where you can get the references for any of above mentioned Http clients: How to check the status of POST endpoint/url in java

Does HTTP stub server stubby4j support request proxying with additional query params setting?

TL;DR:
Does stubby4j request proxying functionality support the setting of additional query params with the request which is being proxied?
Details:
I am using stubby4j HTTP stub server, the latest version (i.e.: v7.3.3) to proxy requests to another real live service when my request did not match any of the configured stubs.
I am interested to know if it is possible to pass in additional query params to the live service with the request which is being proxied?
In the official docs of the stubby4j request proxying behavior (https://stubby4j.com/docs/request_proxying.html) there is nothing mentioned about it and from what I see in my own testing, I do not think this is supported. But, I still wanted to ask on SO to check if I am simply doing something wrong.
You are correct, the the setting of additional query params on the request being proxied is not supported currently.
As per the aforementioned docs, the additive strategy only supports the setting of additional HTTP headers, which are specified in the headers property on the proxy-config object in your YAML.
But, it is pretty straightforward to add the addition of query params behavior. Feel free to raise a feature request at https://github.com/azagniotov/stubby4j/issues/new/choose

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

Spring sessionid in request body xml instead of JSESSIONID cookie

Most of my Spring application uses regular spring security. However, there is a (non-negotiable) requirement for certain pages to be accessed by an embedded system that communicates exclusively via XML inside a POST body. That is to say, it's not capable of passing 'JSESSIONID=xxx' or receiving cookies, but instead it expects to receive a sessionid via xml on login, which it then repeats back in future requests.
So, on login (I've simplified here), it sends
<username>user</username><password>password</password>
in a post body to http://example.com/xml_login and expects
<sessionid>xxxxxx</sessionid>
in reponse. Then all subsequent requests will repeat back the <sessionid> tag's contents as part of its body.
Any ideas how to go about getting spring security to use this?
My first attempt was to try writing a filter to extract the sessionid and crowbar a JSESSION= into the body, but it turned out to be too late (by the time I had a request object to get the body from, spring security had already established the session, and I can't see any way of doing anything like 'request.setSession(id)'). The only other thing I can think of is to write my own session handling outside of spring security, but it'd be nice if I didn't have to do that.
thanks

IIS URL Rewrite - Convert POST to GET

In my application there is a client and a WCf REST service. For invoking some wcf service the client is doing an http POST even though the service is a GET.
i do not want to do any changes in the client or the service.
So is there a way where i can convert this POST request to GET and add the data coming in as the POST to the URL and invoke the REST service.
Thanks in advance.
You can use URL Rewrite to issue 3xx Redirect which will use GET method, but you will loose all POST data.
The only safe way known to me is to rewrite POST request to some another custom page, where you:
collect all POST data/variables;
convert them into GET variables (assemble proper GET request);
issue 301 (or 302) Redirect to the proper URL (it will have all POST data sent as GET variables).
Such rewrite to custom page should be easy -- you need to check what method is used (POST or GET) and only invoke it on POST. The rest will be handled in that post-to-get script.
The reason for all of this complexity is the difference in how POST and GET requests work: with GET all data is sent as part of URL while POST uses request body to transfer variable's data.

Resources