title: How to make POST request in Spring REST service
body: <p>I have a service rest and I want to do a POST to a URL. When I check with POSTMAN this url works. </p>
  
  <p>With POSTMAN, the request is </p>
  
  <p>{</p>
  
  <pre><code>"username": "RenfeInbound",
  
  "password": "renfeinbound"
  </code></pre>
  
  <p>}</p>
  
  <p>This is my spring service rest</p>
  
  <pre><code>@Override
      public RespuestaTransfesaBean peticionLogin(PeticionLoginTransfesa datosLogin) {
  
          RespuestaTransfesaBean res = null;
          try {
  
              RestTemplate restTemplate = new RestTemplate();
  
              HttpEntity&lt;PeticionLoginTransfesa&gt; request = new HttpEntity&lt;&gt;(datosLogin, getHttpHeaders());
              res = restTemplate.postForObject(TRANSFESA_URL, request, RespuestaTransfesaBean.class);
  </code></pre>
  
  <p>TRANSFESA_URL is the URL where I want to do the request. </p>
  
  <p>datosLogin are the request values</p>
  
  <p>Method getHttpHeaders() is</p>
  
  <pre><code> private HttpHeaders getHttpHeaders() {
              if (headers == null) {
                  headers = new HttpHeaders();
                 // headers.add("username", TRANSFESA_USER);
                 // headers.add("password", TRANSFESA_PASS);
                  headers.add("Content-Type", "application/json");
              }
  
              return headers;
          }
  </code></pre>
  
  <p>When I execute the request, I get the following error on the console</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://cloud-uat.transfesa.com/renfe-int-api/login": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
      at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:413)
      at com.renfe.bcm.externas.transfesa.service.TransfesaServiceImpl.peticionLogin(TransfesaServiceImpl.java:35)
      at com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController.loginTransfesa(TransfesaRestController.java:23)
      at com.renfe.bcm.externas.transfesa.restcontroller.TransfesaRestController$$FastClassBySpringCGLIB$$9aa75999.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
  </code></pre>