title: Rest Controller method that returns a token by calling two service methods that requires rest template calls to an external api
body: <p>I want to return a token response in my controller, my rest controller method is calling two services which are  implementing Rest Template post method to call external apis.
  My tokenservice layer requires a parameter &quot;code&quot; to be passed into it.</p>
  <p>When I try to write my rest controller method to implement my codeservice and only return string &quot;code&quot; back, the code is returned from the external Api.</p>
  <p>But when I try to run the method implementing get code and return token in a single rest endpoint I get errors :</p>
  <p>Stack trace:
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785)
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)</p>
  <p>Do I need to pass in both the Body(x-www-form-urlencoded and raw body) in a single call?</p>
  <pre><code>//RETRIEVE CODE SERVICE //
  @Override
  public String retrieveCode(String client_id, MUser mUser) {
  
      String bUrl = &quot;xxx&quot;;
      String url = UriComponentsBuilder.fromUriString(bUrl)
      .queryParam(&quot;clientId&quot;, client_id)
      .build()
      .toUriString();
  
      HttpEntity&lt;MUser&gt; httpEntity  = new HttpEntity&lt;&gt;(mUser, getHttpHeaders());
      String code = &quot;&quot;;
  
      try {
          ResponseEntity&lt;MR&gt; responseEntity = this.restTemplate.postForEntity(url, httpEntity, MR.class);
          code = responseEntity.getBody().getCode();
      } catch(HttpClientErrorException | HttpServerErrorException httpException) {
          log.error(&quot;Could not retrieve code: {}&quot; httpException.getMessage());
      }
  
      return code;
  }
  
  protected HttpHeaders getHttpHeaders() {
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
      headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
      return headers;
  }
  
  //TOKEN SERVICE //
  @Override
  public Token retrieveToken(String code, boolean c1, boolean c2) {
  
      String url = UriComponentsBuilder.fromUriString(authProperties.getTokenUrl())
      .build().toUriString();
  
      HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; entity = getHttpEntity(code, p1, p2);
  
      ResponseEntity&lt;Token&gt; responseEntity = this.restTemplate.postForEntity(url, entity, Token.class);
      return responseREntity.getBody();
  }
  
  protected Http&lt;MultiValueMap&lt;String, String&gt;&gt; getHttpEntity(String code, boolean c1, boolean c2Refresh) {
      MultiValueMao&lt;String, String&gt; map = new LinkedMultiValueMap();
  
      if(c1 || c2Refresh) {
  
          map.add(CLIENT_ID, this.authTokenServiceConfig.getC1ClientId());
          map.add(CLIENT_SECRET, this.authTokenServiceConfig.getC1ClientSecret());
  
      } else {
          map.add(CLEINT_ID, this.authTokenServiceConfig.getFClientId());
          map.add(CLIENT_SECRET, this.authTokenServiceConfig.getFClientSecret());
      }
  
      map.add(CODE, code);
      map.add(GRANT_TYPE, AUTHORIZATION_CODE);
      map.add(REDIRECT_URI, authProperties.getRegisteredRedirectURL())
  
      return new HttpEntity&lt;&gt;(map, getHttpHeaders());
  }
  
  
  protected HttpHeaders getHttpHeaders() {
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
      headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
      return headers;
  }
  
  
  // REST CONTROLLER METHOD //
  
  @PostMapping(value = &quot;/mauth&quot;) 
  public Token mAuthorize(@RequestParam(required= true) String clientId, @RequestBody MUser mUser) {
  
      String code = authService.retrieveCode(clientId, mUser);
      Token token = authTokenService.retrieveToken(code, false, false); // when I comment this line out and return just the String code It works properly 
  
      return token;
  }
  
  </code></pre>
  <p>enter image description here</p>