Spring resttemplate InvalidMediaTypeException. Invalid mime type due to Invalid token character '#' in token - spring

The api I am calling responds with a content-type that has # in the value for in response to my resttemplate call.
final HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, "key=abcd#def");
final ResponseEntity<String> responseEntity = resttemplate.exchange(
path, HttpMethod.GET, new HttpEntity<>(null, headers),
String.class);
Response Header
content-type: key=abc#def
Error I get
org.springframework.http.InvalidMediaTypeException:
Invalid mime type "key=abcd#def": Invalid token character '#' in token "abcd#def"

Related

How to read response with Content-Type text/plain;charset=UTF-8 using RestTemplate

I have an API (/get-sas-token) that is returning a response with Content-Type=text/plain;charset=UTF-8.
In Postman when I hit this API it returns as sig=PAd%2By7yzue0G%2FVeMKbwvR%2F%2B5a3X8CUTablCIhS3uCuk%3D&s
The code for this API is
#GetMapping("/get-sas-token/{containerName}")
public String getSASToken(#PathVariable("containerName") String containerName)
throws InvalidKeyException, URISyntaxException, StorageException {
CloudBlobContainer appcontainer = config.blobClient().getContainerReference(containerName);
return appConfiguration.generateSASToken(appcontainer);
}
My other microservice is trying to call this RestAPI using RestTemplate via a get request. The response I'm getting in Postman has instead of special characters like �������{J�J��t�\b�`$ؐ#������iG# some weird stuff. So I guess somethings wrong with the character encoding.
P.S : If I remove the Accept-Encoding(gzip, deflate, br) header from Postman it works.How can i get it working in my code
ResponseEntity<String> imageUrl = restTemplate.exchange(
fileServiceUrl + "/get-sas-token/" + containerReference, HttpMethod.GET, request, String.class);
logger.info("imageUrl body-------->>" + imageUrl.getBody());
It prints
imageUrl body-------->>?`I?%&/m?{J..................
I have tried all the possible soultions but nothing works for me
tried this::
template.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
ResponseEntity<Object> response = template.exchange(endpoint, method, entity,
Object.class);
Also this::
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Accept", "application/json");

Spring Boot RestTemplate WebClient - Response Body JSON Empty

I am using Rest Template and have used Web Client as well. I get the key in response but the body is empty always. Using Postman I can see the Response which is a JSON.
My code snippet is below -
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("john", "doe");
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(
"https://www.getMeData.com",
HttpMethod.GET, request, String.class);
System.out.println(response.getBody());
Response is as below -
{"result":[]}
Has anyone faced the same issue?
In Postman the Response Headers I can see is -
KEY VALUE
Content-Encoding gzip
Content-Type application/json;charset=UTF-8
Transfer-Encoding chunked
I just changed the Restemplate exchange method parameter from String url to new URI("url") and it worked.
ResponseEntity<String> response = restTemplate.exchange(new URI("https://www.getMeData.com"),
HttpMethod.GET, request, String.class);
Try to add headers as below:
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("john", "doe");
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<String>(headers);
For gZip - use
Gzip is disabled by default, check whether it's enabled on your application. If not enable Gzip with properties
server.compression.enabled=true
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
server.compression.min-response-size=1024
And add below encoding to test
headers.add(HttpHeaders.CONTENT_ENCODING, "gzip");
headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
You can also try with creating a new Http message convertor and add the same to RestTemplate instance before invoking remote api call..
Spring implementation simply does not support it.
From the HTTP 1.1 2014 Spec:
A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.
Having that stated this is how you should refactor your code to make it work:
ResponseEntity<String> response = restTemplate.exchange(
"https://www.getMeData.com",
HttpMethod.POST, // <- this is it
request, String.class);
If you have no control over controller method layout - switch to python + requests library to have this done.

Spring RestTemplate exchange return 204 and in postman 200

I'm turning around since this morning , i've this method that return NO_CONTENT (204) in Java, but when i execute the same URL in postman i got Content (200)
String encodedCredentials = getEncodedCredentials(deviceConfigurationInstance);
RestTemplate restTemplate = getRestTemplate(url, deviceConfigurationInstance);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + encodedCredentials);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "*/*");
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, request, retType);
return response;
I've add the credential in postman also
What i've forgot in Java ?
Note : retType has Class<T> as type

How to not escape form body characters in Spring RestTemplate when making a POST request?

I have following code where I am making a POST form request. The request body contains username and password. Password contains # characters, which is replaced by RestTemplate with %40 and I am getting "unauthorized" error as the password is wrong now.
Following is the debug info from bufferOutput(request body)
merchant_id=firstname+de-lastname%40gmail.com&password=%40Password
Here is the code snippet that is making the call.
MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
String url = "SOME_URL";
formData.add("username", "xay#gmail.com");
formData.add("password", "#name321");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String,String>> requestEntity = new HttpEntity<MultiValueMap<String,String>>(formData,headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
restTemplate.exchange(url, HttpMethod.POST, requestEntity, Authentication.class,"321");
The question is how to tell restTemplate not to escape body data ?
Note that the request has to be a post form request and I can not use UriComponentsBuilder to fix the problem.
I think you need to add a FormHttpMessageConverter to your RestTemplate rather than a StringHttpMessageConverter.

RestTemplate POST request urlformencoded gives 400 (Bad Request)

I have the following request :
String url = "url to oauth_token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RestTemplate restTemplate = new RestTemplate();
String body = "grant_type=authorization_code&client_id=123&client_secret=123&"
+ "redirect_uri=https://axyz.com&code=123";
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
Object token = restTemplate.exchange(url, HttpMethod.POST, entity, Object.class);
This seems to return 400 (Bad Request). I have also tried its alternatives where body is a MultiValueMap but this is what makes the most sense to me. Is there something wrong with the way I am trying the request?
The values of the POST fields should be URL encoded (you can use URLEncoder.encode(value, "UTF-8") for each value while concatenating the body string). That's why you get 400 error.
You'd better use a more convenient way to create a POST form entity with keys and values, which will URL encode your values automatically:
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("redirect_uri", "https://axyz.com"));
...
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);

Resources