Spring boot resttemplate get request found 401 unauthorized exception - java

Following code snippet receives 401 unauthorized exception :
String uri = "https://api/example.json?status=all";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);
ResponseEntity <String> results = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class;

Related

Retrieve a cookie using Spring RestTemplate

I need to get a cookie from a server using Spring RestTemplate. Do you guys know how I can perform this?
Thank you for your help!
final String url = "http://codeflex.co:8080/rest/Management/login";
RestTemplate template = new RestTemplate();
Credentials cred = new Credentials();
cred.setUserName("admin#codeflex.co");
cred.setPassword("godmode");
HttpEntity<Credentials> request = new HttpEntity<>(cred);
HttpEntity<String> response = template.exchange(url, HttpMethod.POST, request, String.class);
HttpHeaders headers = response.getHeaders();
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
code from the example
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue());
HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
ResponseEntity rssResponse = restTemplate.exchange(
"https://jira.example.com/sr/jira.issueviews:searchrequest-xml/18107/SearchRequest-18107.xml?tempMax=1000",
HttpMethod.GET,
requestEntity,
Rss.class);
Rss rss = rssResponse.getBody();
from http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate

Spring RestTemplate UnsupportedOperationException with Collections$UnmodifiableCollection.add(Unknown Source)

This is our rest template config
#Bean
public RestTemplate infoBloxRestTemplate() {
RestTemplate restTemplate=new RestTemplate();
ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(httpBasicAuthenticationInterceptor());
restTemplate.setInterceptors(interceptors);
restTemplate.getMessageConverters().add(jacksonConverter());
restTemplate.setRequestFactory(genericHttpRequestFactory());
return restTemplate;
}
We are trying to make a POST call which works successfully with Postman and returns proper response.
final HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");
HttpEntity<Object> httpEntity = new HttpEntity<Object>(record, headers);
StringBuilder uri = new StringBuilder(infobloxRestClient.createUrl("/record:host"));
infobloxRestClient.getRestTemplate().exchange(uri.toString(), HttpMethod.POST, httpEntity, String.class);
But this POST invocation fails with below error. Here is my stack trace:
com.sun.xml.ws.server.sei.TieHandler createResponse
SEVERE: null
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
at org.springframework.http.HttpHeaders.add(HttpHeaders.java:558)
at com.test.externalinterfaces.HTTPBasicAuthenticationInterceptor.intercept(HTTPBasicAuthenticationInterceptor.java:30)
at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:81)
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:67)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
Any help on this regard will be very helpful.
To get more people know this issue:
#Sameer, I found the problem is you're using HttpHeaders ,you can try to build your header with this code
MultiValueMap<String, String> headers =new LinkedMultiValueMap<String, String>();
And not use the HttpHeaders and then in your HttpEntity to build the object entity as
new HttpEntity<Object>(record, headers);
Then it should solve problem .
Try creating the RestTemplate object in the following way,
ResTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", authorizationProperty); //Can also use add(String headerName, String headerValue)
This way it should works.

Getting "400 Bad Request" when I post shipping status to Shopify

I try to send the shipping status to Shopify but I get:
400 bad request error.
Does anyone have any ideas?
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.add("X-Shopify-Access-Token", "9a4d5c56a7edf1ac5bb17aa1c");
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity formEntity = new HttpEntity(headers);
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<String> responseEntity = restTemplate.exchange("https://mysite.myshopify.com/admin/orders/406287121/fulfillments.json",HttpMethod.POST, formEntity,String.class);
System.out.println("Response="+responseEntity.getBody());

HTTPClientErrorException: 400 Bad Request using Spring REST template

String Stringifiedjson = new Gson().toJson(user);
Log.d("SpringAndroid", Stringifiedjson);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new
MediaType("application", "json")));
HttpEntity <? > requestEntity = new HttpEntity < Object >
(Stringifiedjson, requestHeaders);
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List < HttpMessageConverter <? >> messageConverters =
new ArrayList < HttpMessageConverter <? >> ();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
try {
ResponseEntity<String> responseEntity = restTemplate.
exchange(url, HttpMethod.POST, requestEntity, String.class);
}
I am getting a 400 Bad Request, but when i send the Stringifiedjson in my fiddler. I get a valid response. Now how to post the GSON Object using RestTemplate
I had this same problem. I solved it by passing the Object itself instead of a String containing the JSON representation of that Object.
In short, I changed:
String jsonObject = convertToJSON(myObject);
HttpEntity <? > requestEntity = new HttpEntity <String>
(jsonObject, requestHeaders);
To:
HttpEntity <? > requestEntity = new HttpEntity <MyObject>
(myObject, requestHeaders);
MyObject in this case is the java bean I want to be catched in the #RequestBody parameter of the Controller.
I think the problem could be with your http request headers
The below worked for me.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

Spring RestTemplate postForObject with Header: webservice can't find my header parameters

I'm struggling with RestTemplate. I need to POST some authentication information to a rest webservice. I can send a request and I get a response. But according to the response my header parameters are not getting through. (Sending the same request with SOAPUI works fine)
This is my code:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("companyId", companyId);
headers.add("password", password);
HttpEntity<String> request = new HttpEntity<String>(headers);
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
LoginResponse response = (LoginResponse)restTemplate.postForObject(url, request, LoginResponse.class);
Anyone who can tell me what's wrong with my HttpEntity or HttpHeader?
thank you.
SOLVED:
Ok, finally got it working.
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("companyId", companyId);
map.add("password", password);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJacksonHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
LoginResponse response = (LoginResponse) restTemplate.postForObject(url, request, LoginResponse.class);
Because I also had a hard time on the response, maybe it can be useful to others:
#JsonIgnoreProperties(ignoreUnknown = true)
public class ItcLoginResponse {
public String loginToken;
#JsonProperty("token")
public String getLoginToken() {
return loginToken;
}
public void setLoginToken(String loginToken) {
this.loginToken = loginToken;
}
}
You're setting a header indicating you're posting a form (APPLICATION_FORM_URLENCODED), but then setting companyId and password as HTTP headers.
I suspect you want those fields to be in the body of your request.
You're also creating an HttpEntity<String> which indicates you're going to post a request body containing a String, but you're providing headers but no String.
If that doesn't help you fix it perhaps you can explain what the request is supposed to look like.

Categories