能否用Dredd工具连接公共API端点?连接报错EAI_AGAIN求助
Absolutely! Dredd is fully designed to work with public remote API endpoints—it’s one of its key use cases for validating that your API contract matches the behavior of a live production or third-party service. That said, the Error:getaddrINFO EAI_AGAIN you’re hitting is a DNS resolution failure, which means Dredd can’t resolve the domain name of your target endpoint. Let’s break down how to fix this:
Common Fixes for EAI_AGAIN Error with Public Endpoints
1. Ensure Your Endpoint URL Includes the Full Protocol Prefix
This is the most common culprit. Your example endpoint api.openweathermap.org/data/2.5/weather?lat=35&lon=139 is missing the http:// or https:// prefix. Without this, Dredd treats the entire string as a hostname to resolve, which fails (since api.openweathermap.org/data/2.5/weather?lat=35&lon=139 isn’t a valid domain).
Fix this by using the complete URL in your Dredd config:
- In your
dredd.yml:endpoint: "https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139" - Or when running Dredd via command line:
dredd your-contract.yml "https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"
2. Verify DNS Resolution in Your Environment
Since Postman works, but Dredd doesn’t, check if your terminal environment can resolve the domain:
- Run
ping api.openweathermap.orgin the same terminal you use to launch Dredd. If this fails, your system’s DNS settings are the issue. Try switching to a public DNS like Google’s8.8.8.8or Cloudflare’s1.1.1.1. - If ping works but Dredd still fails, test with
curlto confirm connectivity:
If curl succeeds but Dredd doesn’t, check for proxy settings (see next point).curl "https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"
3. Configure Proxy Settings (If Needed)
If your network uses a proxy, Postman might be configured to use it automatically, but Dredd won’t unless you explicitly set it:
- Set proxy environment variables before running Dredd:
export HTTP_PROXY=http://your-proxy-address:port export HTTPS_PROXY=http://your-proxy-address:port dredd your-contract.yml "https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139" - Or pass the proxy directly via Dredd’s command line flag:
dredd your-contract.yml "https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139" --proxy http://your-proxy-address:port
4. Update Dredd and Node.js
Older versions of Dredd or Node.js can have bugs related to DNS resolution or network requests:
- Update Dredd to the latest version:
npm install -g dredd - Ensure you’re running a supported Node.js LTS version (16.x or newer is recommended).
5. Escape Special Characters in the URL
If your endpoint includes query parameters, make sure they’re properly handled by your terminal. For example, the & character in your query string might be interpreted as a command separator. Wrapping the entire URL in quotes (as shown in earlier examples) fixes this.
内容的提问来源于stack exchange,提问作者sachu

