k6 HTTP Request DNS Resolution Failure Fix
In this tutorial, you'll learn about k6 http request dns resolution failure fix. We cover key concepts, practical examples, and best practices.
Your k6 HTTP request fails with dial tcp: lookup api.example.com: no such host — the virtual user cannot resolve the target domain, the DNS resolver is misconfigured, or the network cannot reach the endpoint.
The Problem
import http from 'k6/http';
export default function () {
const res = http.get('https://api.example.com/users');
console.log(`Status: ${res.status}`);
}
ERRO[0001] dial tcp: lookup api.example.com: no such host
WARN[0001] Request Failed
The script uses a domain that k6's DNS resolver cannot find, or the test environment lacks DNS resolution for internal hostnames.
Step-by-Step Fix
1. Verify DNS resolution outside k6
nslookup api.example.com
dig +short api.example.com
If DNS fails outside k6, the issue is with the network or DNS server, not k6.
2. Use an IP address or override DNS
import http from 'k6/http';
export const options = {
hosts: {
'api.example.com': '203.0.113.50',
},
};
export default function () {
const res = http.get('https://api.example.com/users');
console.log(`Status: ${res.status}`);
}
3. Configure custom DNS resolver
import http from 'k6/http';
import { DNS } from 'k6/net';
export const options = {
dns: {
servers: ['8.8.8.8', '1.1.1.1'],
ttl: '30s',
timeout: '5s',
},
};
export default function () {
const res = http.get('https://api.example.com/users');
}
4. Set a DNS resolution fallback
# Run k6 with --dns flag
k6 run --dns 8.8.8.8 script.js
# Use a local hosts file
k6 run --hosts /etc/hosts script.js
5. Add connection timeout and retry
import http from 'k6/http';
import { sleep } from 'k6';
const BASE_URL = 'https://api.example.com';
const MAX_RETRIES = 3;
export default function () {
for (let i = 0; i < MAX_RETRIES; i++) {
const res = http.get(BASE_URL + '/users', {
timeout: '10s',
});
if (res.status === 200) {
console.log(`Attempt ${i + 1}: OK`);
break;
}
console.log(`Attempt ${i + 1}: failed, retrying...`);
sleep(1);
}
}
Expected output:
✓ status is 200
✓ response time < 500ms
http_req_duration........: avg=145ms min=132ms med=140ms max=210ms
http_req_failed..........: 0.00% ✓ 0 ✗ 100
Prevention Tips
- Verify DNS resolution independently before running k6 tests
- Use
hostsoption in k6 to override DNS for internal services - Set
dns.ttlanddns.timeoutfor more reliable resolution - Use IP addresses for stable internal endpoints
- Add retry logic for transient DNS failures
Common Mistakes with http request
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world K6 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro