How to Pass a RequestBody in DeleteMethod in spring boot - spring-boot

I need to pass a request body to make a delete request in order to call a third-party API's delete Request. Is it possible?

The specification RFC 7231 doesn't prevent from accepting a RequestBody in DELETE method.
A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.
You can accept Request Body if the underlying web server is configured to parse the body for DELETE method.

So I have found the way that I was searching for. The problem can be solved using Rest template
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Model> request = new HttpEntity<>(new Model("data"));
restTemplate.exchange(url, HttpMethod.DELETE, request, null);

Related

How to pass header values using RestTemplate?

I am new to Spring. A project of mine needs to call an Autotask API which takes Api integration code, username and secret (password) in the headers. How do I integrate that into the Rest Template?
Below is a screenshot of the API that I am testing on swagger. Screenshot of API Call using Swagger
The additional parameters you need to send are all headers. You can set the headers as follows:
HttpHeaders headers = new HttpHeaders();
headers.set("ApiIntegrationCode", "HUCXSL...");
headers.set("UserName", "fdfsk...");
headers.set("Secret", "yR*42...");
Then you add the headers to a new HttpEntity instance and perform the request using RestTemplate:
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
A very similar question has been asked here: HTTP get with headers using RestTemplate.

Feign Client - Dynamic Authorization Header

I have a service that gets http request with an authorization header.
When processing the request, I want to use a Feign Client to query another service. The query to the other service should include the same authorization header.
Currently I use a Filter to extract the authorization header from the incoming request, store the header in a ThreadLocal.
When building the Feign Client I use a RequestInterceptor to read the authorization header from the ThreadLocal and put it into the request to the other service.
This approach is not ideal, because when I start using things like RxJava or Hystrix, threads are changed while processing the request and I have to move the authorization header ThreadLocal from one thread to another.
What are other options to solve this?
One way that I am thinking about is to create a new FeignClient for each request, this way I would no longer need to store the authorization in a thread local. But is this a good idea?
I think I found a solution for my problem. Using RequestContextHolder I can get a reference to the original request (also from spawned child threads) and copy the header from there:
public class AuthForwardInterceptor implements RequestInterceptor {
#Override
public void apply(RequestTemplate template) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
template.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
}
}

#RequestHeader in spring not accepting fake headers

I am using
#RequestHeader(value = "channel") String channel
in Spring application controller to get headers and it works fine for normal headers, however, in some cases, I need to trap a request using filter and use HttpServletRequestWrapper to add some extra headers. The new headers added by overriding getHeader method are not being accepted by #RequestHeader annotation and throws error. However, if I manually get headers using
HttpServletRequest.getHeader("channel")
the new headers work fine. Is there any bug in #RequestHeader implementation? If so, is there any work-around so that I wouldn't have to change the same thing in 50+ APIs.
When resolving #RequestHeader, Spring is using getHeaders(), not getHeader(). You need to override this one.

How to access previous SOAP headers in SoapInterceptor?

Here's my configuration:
A request is captured by my REST controller, I send data via SOAP to my webservice. Then I access some data sending SOAP request to another service and I return gathered data to the user sending the request.
Before sending SOAP request to external webservice I need to set some headers, so I have an interceptor that extends AbstractSoapInterceptor with Phase.PRE_PROTOCOL in constructor by webservice side.
Inside handleMessage() I create new headers and add to SoapMessage but... the data I need to set is inside REST request inside it's headers. So in order to get them I need to have access to HttpServletRequest and then I just get the header using HttpServletRequest#getHeader("header_name").
I've saw here I could just use #Context annotation but it's not available in my Spring (3.0.5) or maybe RESTEasy is something else and that question isn't connected with mine in anyway
I've tried this:
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
and
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
but it's been a shot in the dark and it failed.
Edit:
Thanks to Abel ANEIROS I've added:
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
and was able to get HttpServletRequest from RequestContextHolder.getRequestAttributes() but apperently it's origin is from my webservice and not REST
Edit2:
The structure is different than I thought.
It's: Rest Controller -> MyWebService (SOAP) -> ExternalService (SOAP)
I've created another interceptor in controller side, added headers to SOAP message and now I'm trying to get the headers again in MyWebService side.

The difference between exchange method and execute method in spring rest template?

I have three questions!
First.
I am using the spring framework for sending the data through rest protocol.
restTemplate.exchange(requestUrl,HttpMethod.POST, request, listVo.getClass());
org.springframework.web.client.RestTemplate.exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<? extends Object> responseType, Object... uriVariables) throws RestClientException
I used it without any problem, but I want to know the purpose of the parameter, responseType.
The client don't use response data, but just use response status code / msg. So, I sent some meaningless
String data instead. But the error thrown that they accept "null". So I sent a "null" String. not null.
Then, the error got rid of. But there was another problem. Right after the client received the data from the server and paused for a long time. Then next line of codes are executed. What is problem?
Second
I can't find any references that use execute method of Spring RestTemplate.
Third
Like the title, What is the difference between the exchange method and the execute method in spring rest template?
Thanks for your time and effort.
Cheers.
exchange return type is ResponseEntity<T> while execute is T
Taken from "Pivotal Certified Spring Web Application Developer Exam" book
The execute and exchange methods can be used for any type of REST calls
The execute method can also be given a RequestCallback implementation as a parameter, which tells the RestTemplate what to do with the request before sending it to the server.

Resources