1

I have a curl command, which is working fine, but when I am trying it in RestTemplate I am getting errors.

Curl:

curl -X POST "https://api.example.com/v1/tokens/accessToken" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"refresh_token\": \"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsIng1dCI6IndKZlQ2NHpqemQxWk4yS1duX3FYSmpvOHByMCIsImtpZCI6Im9yYWtleSJ9.eyJzdWIiOm51bGwsIm9yYWNsZS5vYXV0aC51c2VyX29yaWdpbl9pZF90eXBlIjoiTERBUF9VSUQiLCJvcmFjbGUub2F1dGgudXNlcl9vcmlnaW5faWQiOiJQUk9BQ1QyMDE5ISIsImlzcyI6Ind3dy5vcmFjbGUuZXhhbXBsZS5jb20iLCJvcmFjbGUub2F1dGgucnQudHRjIjoicmVzb3VyY2VfYWNjZXNzX3RrIiwib3JhY2xlLm9hdXRoLnN2Y19wX24iOiJPQXV0aFNlcnZpY2VQcm9maWxlIiwiaWF0IjoxNTk1MjM3NDk0LCJvcmFjbGUub2F1dGgudGtfY29udGV4dCI6InJlZnJlc2hfdG9rZW4iLCJleHAiOjE1OTU4NDIyOTQsInBybiI6bnVsbCwianRpIjoiN2I0YzExZDAtNjRkZi00YjhkLWJkZTYtNmM2ODdkZTY4MDI2Iiwib3JhY2xlLm9hdXRoLnNjb3BlIjoiQXN1cFVzZXJQcm9maWxlLm1lIiwib3JhY2xlLm9hdXRoLmNsaWVudF9vcmlnaW5faWQiOiJNeUFzdXBBUEkiLCJ1c2VyLnRlbmFudC5uYW1lIjoiRGVmYXVsdERvbWFpbiIsIm9yYWNsZS5vYXV0aC5pZF9kX2lkIjoiMTIzNDU2NzgtMTIzNC0xMjM0LTEyMzQtMTIzNDU2Nzg5MDEyIn0.UjJKgHkCypoyIsK7-24vUdX4jT2vkODW56tfwNuAnFqvaS712ZdDAQFiu5wjS9sCajAx0LDoJXQoq5ThVliY71FU9cvwS80SRpXzEIX_TcnnOfNXyZBzTyCqKDPjin0ajZ9Jvc_LNbMMD15i0tN3U6IQHmahm8yikGL7lBtvT30\"}"

Spring Rest Template

    restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

    Map<String, Object> map = new HashMap<>();
    map.put("body", "{\"refresh_token\": \"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsIng1dCI6IndKZlQ2NHpqemQxWk4yS1duX3FYSmpvOHByMCIsImtpZCI6Im9yYWtleSJ9.eyJzdWIiOm51bGwsIm9yYWNsZS5vYXV0aC51c2VyX29yaWdpbl9pZF90eXBlIjoiTERBUF9VSUQiLCJvcmFjbGUub2F1dGgudXNlcl9vcmlnaW5faWQiOiJQUk9BQ1QyMDE5ISIsImlzcyI6Ind3dy5vcmFjbGUuZXhhbXBsZS5jb20iLCJvcmFjbGUub2F1dGgucnQudHRjIjoicmVzb3VyY2VfYWNjZXNzX3RrIiwib3JhY2xlLm9hdXRoLnN2Y19wX24iOiJPQXV0aFNlcnZpY2VQcm9maWxlIiwiaWF0IjoxNTk1MjM4Njc4LCJvcmFjbGUub2F1dGgudGtfY29udGV4dCI6InJlZnJlc2hfdG9rZW4iLCJleHAiOjE1OTU4NDM0NzgsInBybiI6bnVsbCwianRpIjoiMDZiYWE1MjgtMTZmOC00MzU1LWE3ZmYtNDc3ZjA4ZTNmMGU1Iiwib3JhY2xlLm9hdXRoLnNjb3BlIjoiQXN1cFVzZXJQcm9maWxlLm1lIiwib3JhY2xlLm9hdXRoLmNsaWVudF9vcmlnaW5faWQiOiJNeUFzdXBBUEkiLCJ1c2VyLnRlbmFudC5uYW1lIjoiRGVmYXVsdERvbWFpbiIsIm9yYWNsZS5vYXV0aC5pZF9kX2lkIjoiMTIzNDU2NzgtMTIzNC0xMjM0LTEyMzQtMTIzNDU2Nzg5MDEyIn0.UCkScWFn8d3pupwAxilT8T-m6Rf8fgZ8ge9OZvWZbZ-4eCVCPyHeI8A1a2QV0_iZhjwHaITNTzCZ4Eydfkm-mZuCa-PLY63HJhWLzTGlbaYsWRVtHaLF58F6spZ106T6T3ZDvQVIsiQ9845ruG_1ib0vKV_57X_CcVB4XZktYvA\"}");

    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(map, headers);

    String urlString = "https://api.example.com/v1/tokens/accessToken";
    String response = restTemplate.postForObject(urlString, entity, String.class);

Exception:

Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [{"error":"Request Failed"}]

So How to properly convert above curl into RestTemplate to avoid server and no body errors. The Curl command is working fine.

vicky
  • 890
  • 4
  • 23
  • 54
  • map.put("refresh_token", "eyJhbGci..."> when I do this, i get this error: 401 Unauthorized: [no body] – vicky Jul 20 '20 at 14:46
  • 1
    u should use: map.put("refresh_token", "eyJhbGci...". u have 401 because u passed **another** token (check diff cURL vs. java code version) – star67 Jul 20 '20 at 15:05
  • thank you star67, I use the logic with new valid refresh code and it.I got the access token. Have a nice day. please post your answer to be accepted – vicky Jul 20 '20 at 15:19

3 Answers3

1

Try this

    restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = 
             new HttpEntity<>("{\"refresh_token\": \"eyJhb...\"}", headers);
  • Thank you, but, it is also not working.Caused by: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body] – vicky Jul 20 '20 at 15:09
1

as you correctly noticed, you should use:

map.put("refresh_token", "eyJhbGci...". 

but u have 401 just because u passed another token (check diff cURL vs. java code version)

star67
  • 1,505
  • 7
  • 16
0

check your token if the token has a type for example Bearer you must write so:

"Authorization": "Bearer rrdedzfdgf........."

and make sure that there is only one space between Bearer and the token

Ballo Ibrahima
  • 461
  • 4
  • 10