How to pass header values using RestTemplate? - spring

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.

Related

Spring RestTemplate exchange returns 204 while trying to download text/csv from GET endpoint

I am trying to download text/csv file from one of the Salesforce GET endpoint using Spring RestTemplate.
I am getting 204 no-content when trying from code. While I am trying to hit the same endpoint with from Postman (Send and download) its working fine.
I am using the below code :
HttpHeaders headers=new HttpHeaders();
headers.setAccept(Mediatype.APPLICATION_OCTET_STREAM);
headers.add("Authorization","");
HttpEntity<String> entity=new HttpEntity<>(headers);
ResponseEntity<byte[]>
response=template.build.exchange(url,HttpMethod.GET,entity,byte.class);
System.out.println(response);
To my surprise, when I am debugging the application and giving a debug point on the line where the GET call is made, the response is coming properly.
Looks like some buffering is required while calling the GET endpoint.
I have tried to use BufferingClientHttpRequestFactory like below :
ClientHttpRequestFactory factory=new BufferingClientHttpRequestFactory(new
HttpComponentsClientHttpRequestFactory);
RestTemplate template=new RestTemplate (factory);
Even with the above am getting the same 204 content.

How to Pass a RequestBody in DeleteMethod in 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);

spring RestTemplate vender specific Media type headers not working

I am trying to convert my jersey api call to spring boot resttemplate client call, when i am trying to add vender specific header its saying unsupported media type.
i tried like this
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.ALL));
headers.setContentType(MediaType.ALL);
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<Admin> result = restTemplate.exchange(url, HttpMethod.GET, entity, Admin.class);
i even tried diffrent combinations of Media types but it did not worked i also tried to add media type like MediaType.parseMediaType("application/vnd.....);
please provide some help in this.please let me know what is httpMessage converter and how to add this to our custom vender specific media types.
You won't be able to set a custom content type with setContentType(MediaType mediaType) since it accepts a MediaType object, which your custom MediaType cannot be converted into.
You can use the below to set the custom content type:
headers.set(HttpHeaders.CONTENT_TYPE,"application/custom");
Use:
headers.setContentType(MediaType.valueOf(VENDOR_MEDIA_TYPE));

Swagger with spring boot microservice

I have a microservice-A which gets the token as a header from another microservice-B. Now I want to implement swagger2 in microservice-A. The problem is every request flows through microservice-B. So swagger-ui throws error in local as
it is not able to get those header parameter which microservice-B is
trying to fetch.
It is not able to get those header parameter which microservice-B is trying to fetch.
Swagger on its own can't call the authenticator service and add the obtained token to another request's header.
You can modify the Docket object to accept additional parameter in header as below:
docket.globalOperationParameters(
Collections.singletonList(new ParameterBuilder()
.name("Authorization")
.description("Bearer [token]")
.modelRef(new ModelRef("string"))
.parameterType("string")
.required(true)
.build()
)
);
This will allow Swagger UI to show additional field to accept token (see image below). You need to obtain the token by yourself and can put in this field.
Hope this helps.

404 when calling post from resttemplate

I am attemtping to call a service using rest template and I am receiving a 404 error.
It's a POST. The signature of method is
String sendScreenAsPostcard(#RequestBody MultiValueMap<String, Object> params)
I am attenpting to call this from the resttemplate with the following code.
restTemplate.exchange(
"http://localhost:8080/sendScreeenAsPostCard",
HttpMethod.POST,
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers),
String.class
).getBody()
Please advice what I am doing wrong. Thanks.
404 means the resource isn't found. If everything is fairly straightforward, it means there's an HTTP server running at localhost:8080, but no resource available at /sendScreenAsPostCard.

Resources