Home > Software engineering >  Java & Docker app can not hit external API
Java & Docker app can not hit external API

Time:12-30

I have a problem connecting to an external API from a Java application built using Spring boot and feign. The application is deployed to a VM in a docker container. I'm trying to hit an external service endpoint to retrieve some data with no luck so far.

I have executed the same command with curl both from the VM command line and from inside the docker container also from command line but when trying to hit it from the java code I just receive a timeout.

Any ideas about what can be happening, apparently the hosts file and proxy are correct giventhat I can hit the endpoint via curl, so I guess I'm missing something in the code itself or the setup for java.

@GetMapping(value = "${url}", headers = {
            "X-API-Key=${apiKey}",
            "Authorization=${authorization}",
            "Accept=application/json",
            "Content-Type=application/json; charset=utf-8"
    })
    CheckResponse checkEmail(@RequestParam("email") String email) throws FeignClientException;

The curl request that works from the docker and VM.

curl --location --request GET 'https://${url}/[email protected]' --header 'X-API-Key: ${apiKey}' --header 'Content-Type: application/json; charset=utf-8' --header 'Accept: application/json' --header 'Authorization: ${Authorization}'

CodePudding user response:

I think the problem might be in the connection to the external API from a vm. When you start a DockerContainer you can specify the ports beeing map to from the vm to your localmachine using the -p Parameter. For detailed information you may wanna check the docs: https://docs.docker.com/engine/reference/commandline/port/

CodePudding user response:

We managed to fix it by adding the proxy server config to the feign config. Apparently even when the proxy was set to work for the VM & the docker container the java code needed it explicitly.

  • Related