Nabigong kahilingan sa pag-debug

Huling na-update: 3 buwan ang nakalipas

Kapag nabigo ang mga kahilingan, nakatutulong ang sistematikong pag-debug upang mabilis na matukoy ang isyu.

I-enable ang detalyadong logging

async function debugRequest(url, options) {
  console.log('=== API Request ===');
  console.log('URL:', url);
  console.log('Method:', options.method);
  console.log('Headers:', {
    ...options.headers,
    'x-api-key': options.headers['x-api-key']?.substring(0, 10) + '...'
  });
  console.log('Body:', options.body);
  
  const startTime = Date.now();
  
  try {
    const response = await fetch(url, options);
    const duration = Date.now() - startTime;
    
    console.log('=== API Response ===');
    console.log('Status:', response.status, response.statusText);
    console.log('Duration:', duration + 'ms');
    console.log('Headers:', Object.fromEntries(response.headers));
    
    const data = await response.json();
    console.log('Body:', data);
    
    return { response, data };
    
  } catch (error) {
    console.error('=== Request Failed ===');
    console.error('Error:', error.message);
    console.error('Duration:', Date.now() - startTime + 'ms');
    throw error;
  }
}

Suriin muna ito

1. Tama ang API key:

console.log('API key starts with:', apiKey.substring(0, 10));
console.log('API key length:', apiKey.length);

2. Eksakto ang pangalan ng header:

// Dapat ay lowercase na 'x-api-key'
headers: {
  'x-api-key': apiKey,  // ✓ Tama
  'X-API-Key': apiKey,  // ✗ Mali
  'api-key': apiKey,    // ✗ Mali
}

3. Wasto ang JSON:

try {
  const body = JSON.stringify({ query: 'test' });
  console.log('Valid JSON:', body);
} catch (e) {
  console.error('Invalid JSON:', e);
}

4. Naabot ang server ang kahilingan:

// Gamitin ang browser DevTools Network tab o
curl -v -X POST https://api.cathopic.com/resources \
  -H "x-api-key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "test"}'

Mga karaniwang pagkakamali

Maling Content-Type:

// ✗ Mali
headers: {
  'Content-Type': 'text/plain'
}
// ✓ Tama
headers: {
  'Content-Type': 'application/json'
}

Hindi ginagawang string ang body:

// ✗ Mali
body: { query: 'test' }
// ✓ Tama
body: JSON.stringify({ query: 'test' })

Gumagamit ng GET imbes na POST:

// ✗ Lahat ng endpoint ay nangangailangan ng POST
method: 'GET'
// ✓ Tama
method: 'POST'

Subukan gamit ang minimal na kahilingan

Ibinawas sa pinakamaliit upang maihiwalay ang isyu:

const response = await fetch('https://api.cathopic.com/resources', {
  method: 'POST',
  headers: {
    'x-api-key': 'YOUR_ACTUAL_KEY_HERE',
    'Content-Type': 'application/json'
  },
  body: '{}'  // Walang laman na object
});
console.log(response.status);
console.log(await response.json());

Kung gumagana ito, unti-unting idagdag muli ang mga parameter hanggang sa muling mabigo.

Ihambing sa gumaganang halimbawa

Subukan gamit ang curl upang ma-verify ang iyong pagpapatupad:

curl -X POST https://api.cathopic.com/resources \
  -H "x-api-key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "Jesus", "page": 1, "page_size": 5}'

Kung gumagana ang curl pero hindi ang code mo, ihambing ang eksaktong headers at body.

Naka-stuck pa rin?

Makipag-ugnayan suporta ng developer na may:

  • Buong mensahe ng error

  • Mga header ng kahilingan (itago ang iyong buong API key)

  • Body ng kahilingan

  • Katayuan at body ng tugon

  • Wikang pangprograma at HTTP library

  • Snippet ng code (nang hindi inilalantad ang mga kredensyal)

Nakatulong ba ang artikulong ito?