Skip to content
intermediate Phase 76 · Debugging Advanced

Network Debugging

45m
1 problems
Topic Progress 0%

API Debugging

REST API Debugging

# Debug with verbose output
curl -v -X GET 'https://magento.example.com/rest/V1/products' \
  -H 'Authorization: Bearer YOUR_TOKEN'

# Response includes:
# - Request headers
# - Response headers
# - Status code
# - Response body

Common API Errors

// 401 Unauthorized
// - Token expired
// - Invalid token format
// Solution: Refresh token

// 403 Forbidden
// - Insufficient permissions
// - ACL restrictions
// Solution: Check integration permissions

// 404 Not Found
// - Invalid endpoint
// - Resource doesn't exist
// Solution: Verify URL and resource ID

// 500 Server Error
// - PHP error
// - Database issue
// Solution: Check var/log/exception.log

API Request Logging

// Enable API logging
// app/etc/env.php
return [
    'system' => [
        'default' => [
            'web' => [
                'debug' => [
                    'api_log' => 1,
                ],
            ],
        ],
    ],
];

// Check logs
// var/log/api.log

Key Points

  • Use -v flag with curl for verbose output
  • Check request/response headers
  • Verify token format (Bearer prefix)
  • Monitor API logs for debugging

Request/Response Inspection

Request Headers

# Common Magento request headers
POST /rest/V1/orders HTTP/1.1
Host: magento.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Accept: application/json
User-Agent: Magento/2.4.6
X-Requested-With: XMLHttpRequest

Response Headers

# Common Magento response headers
HTTP/1.1 200 OK
Content-Type: application/json
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store
X-Magento-Request-Id: abc123
X-Magento-Tags: catalog_product_1

Response Body Analysis

// Successful response
{
    "id": 1,
    "sku": "TEST-001",
    "name": "Test Product",
    "price": 99.99
}

// Error response
{
    "message": "Product not found",
    "trace": null
}

// Validation error
{
    "message": "Invalid input",
    "errors": {
        "general": ["The SKU is required."]
    }
}

Key Points

  • Headers contain metadata about request/response
  • Content-Type must match payload format
  • Cache-Control affects caching behavior
  • Request IDs help track requests in logs

GraphQL Debugging

GraphQL Query Debugging

# Query with error handling
query {
    products(filter: { sku: { eq: "TEST-001" } }) {
        items {
            name
            sku
            price {
                regularPrice {
                    amount {
                        value
                        currency
                    }
                }
            }
        }
    }
}

GraphQL Error Response

// Query with errors
{
    "data": {
        "products": {
            "items": []
        }
    },
    "errors": [
        {
            "message": "Cannot query field \"invalid_field\".",
            "locations": [{"line": 4, "column": 9}],
            "path": ["products", "items", 0, "invalid_field"]
        }
    ]
}

GraphQL Introspection

# Query schema
{
    __schema {
        queryType {
            fields {
                name
                description
            }
        }
    }
}

# Query type details
{
    __type(name: "ProductInterface") {
        fields {
            name
            type {
                name
                kind
            }
        }
    }
}

Key Points

  • GraphQL returns both data and errors
  • Use introspection to explore schema
  • Check path array for error location
  • Validate query syntax before sending

Troubleshooting Network Issues

Common Issues

// 1. CORS Errors
// Solution: Configure CORS headers
// Access-Control-Allow-Origin: https://your-domain.com

// 2. CSRF Token Errors
// Solution: Include form_key in requests
// X-Requested-With: XMLHttpRequest

// 3. Timeout Errors
// Solution: Increase timeout settings
// nginx: proxy_read_timeout 300s;
// php: max_execution_time 300

// 4. Connection Refused
// Solution: Check service status
// systemctl status nginx
// systemctl status php-fpm

Network Configuration

# nginx.conf for API
location /rest/ {
    proxy_pass http://magento;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    # Increase timeouts
    proxy_connect_timeout 300s;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;
}

Debugging Tools

# Test connectivity
ping magento.example.com
nslookup magento.example.com

# Test ports
telnet magento.example.com 443
nc -zv magento.example.com 443

# Test SSL
openssl s_client -connect magento.example.com:443

# Trace route
tracert magento.example.com

Key Points

  • CORS errors require server configuration
  • CSRF errors need proper headers
  • Timeout issues require timeout configuration
  • Use network tools for connectivity testing

Practice Problems

0 / 1 solved
Debug API Request

Debug a failing REST API request and identify the root cause.

Solution
// Debugging steps:
// 1. Check if Authorization header is present
// 2. Verify token format: Bearer <token>
// 3. Check if token is expired
// 4. Verify API user permissions

// Correct request:
curl -X POST 'https://magento.example.com/rest/V1/products' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIs...'
  -d '{"product": {"sku": "TEST-001", "name": "Test"}}'

// If still 401:
// - Refresh token: POST /rest/V1/integration/admin/token
// - Check token in Magento admin: System > Integrations

Quiz

1. What does 401 Unauthorized mean?

Question 1 options

2. How do you debug API requests?

Question 2 options

3. What is CORS?

Question 3 options

4. How do you test GraphQL schema?

Question 4 options

Flashcards

Question

401 status code?

Answer

Unauthorized - authentication required

Question

Verbose curl flag?

Answer

-v shows request/response details

Question

CORS error solution?

Answer

Configure Access-Control-Allow-Origin header

Question

GraphQL introspection?

Answer

Query __schema to explore GraphQL API

Revision Notes

Key Takeaways

  • 1. Use curl -v for verbose API debugging
  • 2. Check request/response headers for metadata
  • 3. GraphQL introspection explores schema
  • 4. CORS requires proper server configuration

Interview Tips

  • Explain common API error codes
  • Discuss CORS and how to fix it
  • Know GraphQL debugging techniques

Cheat Sheet

Network Debugging

  • curl -v: verbose output
  • Status codes: 200, 401, 403, 404, 500
  • CORS: Access-Control-Allow-Origin
  • GraphQL: introspection query
  • Logs: var/log/api.log