I am running a simple Dart server with package shelf. The server makes a request to generativelanguage.googleapis.com
. Everything is working fine on my own machine. When the Dart program is running on a Ubuntu VPS located in Germany, the API is returning
{ "error": { "code": 400, "message": "User location is not supported for the API use.", "status": "FAILED_PRECONDITION" } }
The VPS is actually in Germany. The Gemini API is supported in Germany. Using tools to find the location by the IP shows Germany.
If I use curl from the VPS, the response is normal, I get the data.
If I do Process.run in Dart and call curl, it works.
If I use dart:http, google_generative_ai package from pub.dev, dio, custom http clients, it returns unsupported user location.
If I run the equivalent code in a python flask server on the VPS, it works.
I suspect there is something wrong in the internals of the dart:http package?
I have tried creating custom Http clients and setting every header possible, I've tried multiple User-Agents, I've tried it with ssl and no ssl, I've tried cors headers, I've tried dockerizing the dart server on my local machine and running it on the VPS, I've tried analyzing the network traffic and it is indeed coming from the correct IP coming from Germany, I've tried running the server on different ports, I've tried running the server behind nginx, I've tried running the server on WSL and it works, I've tried requesting my IP from the Dart application and printing it, I've tried HTTP requests to other APIs from the Dart app and they work fine, I've verified that the API key is correct inside Dart (if I append 'asd' for example I get the correct error back about the API key being invalid).
Edit: I have also tried hosting the Dart server on globe.dev and it works without problem.
Edit 2: Using curl -4 throws the same error about location, using curl -6 works fine. Looks like hetzner ipv4 is blocked?
Edit 3: Disabling ipv4 on ubuntu and/or setting ipv6 precedence to a higher value does not change the default behaviour of dart:http. Manually forcing dart:http to use ipv6 and manually trusting the certificate (since it fails when using https directly to ipv6) is an ugly workaround.
Edit 4: Using a squid proxy and adding the proxy to the http client in dart also works, while everything is using ipv4.
Edit 5: This is a Dart VM issue. Dart first looks up ipv4, then after a delay looks up ipv6. Linking to github issue: https://github.com/dart-lang/sdk/issues/60192
This is a Dart VM issue. Dart first looks up ipv4, then after a delay looks up ipv6. Linking to github issue: https://github.com/dart-lang/sdk/issues/60192
Some workarounds are using a custom http client that manually looks up ipv6 addresses of hosts, or using a proxy in your http client.