Table of contents

How do I use Curl to send requests with query parameters?

Query parameters are a fundamental part of HTTP requests, allowing you to send data to web servers through the URL. When using cURL for web scraping or API testing, properly handling query parameters is essential for successful data retrieval and API interactions.

Basic Query Parameter Syntax

Query parameters are added to the end of a URL after a question mark (?), with multiple parameters separated by ampersands (&). The basic format is key=value.

Simple GET Request with Query Parameters

The most straightforward way to send query parameters with cURL is to include them directly in the URL:

curl "https://api.example.com/search?q=web+scraping&limit=10&format=json"

Important: Always wrap URLs containing query parameters in quotes to prevent shell interpretation of special characters.

Multiple Query Parameters

When sending multiple parameters, separate them with ampersands:

curl "https://api.example.com/users?page=1&size=50&sort=name&order=asc"

URL Encoding and Special Characters

Query parameters must be properly URL-encoded, especially when they contain special characters, spaces, or non-ASCII characters.

Manual URL Encoding

# Spaces should be encoded as %20 or +
curl "https://api.example.com/search?q=hello%20world"

# Special characters need encoding
curl "https://api.example.com/search?query=user%40example.com&type=email"

Using cURL's Built-in URL Encoding

cURL can automatically encode query parameters using the --data-urlencode option with GET requests:

curl -G --data-urlencode "q=hello world" \
     --data-urlencode "category=web scraping" \
     "https://api.example.com/search"

The -G flag tells cURL to append the data to the URL as query parameters instead of sending it in the request body.

Advanced Query Parameter Techniques

Using Arrays in Query Parameters

Some APIs accept array-like query parameters:

# Multiple values for the same parameter
curl "https://api.example.com/filter?tags=javascript&tags=python&tags=curl"

# Array notation (depending on API)
curl "https://api.example.com/filter?tags[]=javascript&tags[]=python"

Complex JSON in Query Parameters

For APIs that accept JSON in query parameters:

# URL-encoded JSON
curl "https://api.example.com/search?filter=%7B%22status%22%3A%22active%22%2C%22type%22%3A%22user%22%7D"

# Using --data-urlencode for better readability
curl -G --data-urlencode 'filter={"status":"active","type":"user"}' \
     "https://api.example.com/search"

POST Requests with Query Parameters

You can combine query parameters in the URL with POST data in the request body:

# Query parameters in URL + POST data in body
curl -X POST "https://api.example.com/users?source=api&version=v2" \
     -H "Content-Type: application/json" \
     -d '{"name":"John Doe","email":"john@example.com"}'

Working with Authentication Parameters

Many APIs require authentication tokens or API keys as query parameters:

# API key as query parameter
curl "https://api.example.com/data?api_key=your_api_key_here&format=json"

# Access token in query parameter
curl "https://api.example.com/protected?access_token=abc123&user_id=456"

Security Note: Be cautious when including sensitive information like API keys in URLs, as they may be logged by servers or visible in browser history.

Handling Dynamic Query Parameters

Using Variables in Shell Scripts

#!/bin/bash
SEARCH_TERM="web scraping"
PAGE_SIZE=25
API_KEY="your_api_key"

curl "https://api.example.com/search?q=${SEARCH_TERM}&limit=${PAGE_SIZE}&key=${API_KEY}"

Building URLs Programmatically

For complex scenarios, you might want to build URLs programmatically:

# Building URL step by step
BASE_URL="https://api.example.com/search"
PARAMS="q=python+scraping&limit=50&format=json"
FULL_URL="${BASE_URL}?${PARAMS}"

curl "$FULL_URL"

Common Patterns and Examples

Search API with Pagination

# Search with pagination and filtering
curl "https://api.example.com/search?q=automation&page=1&per_page=20&sort=relevance&lang=en"

E-commerce Product Filtering

# Product search with multiple filters
curl "https://store-api.example.com/products?category=electronics&price_min=100&price_max=500&brand=apple&in_stock=true"

Social Media API Query

# Social media posts with date range
curl "https://social-api.example.com/posts?user_id=123&start_date=2024-01-01&end_date=2024-12-31&include_replies=false"

Integration with Web Scraping Workflows

When building comprehensive web scraping solutions, cURL with query parameters often serves as a foundation before moving to more sophisticated tools. For dynamic content that requires JavaScript execution, you might need to transition to tools like browser automation frameworks that handle complex interactions.

Error Handling and Debugging

Verbose Output for Debugging

Use the -v flag to see exactly what cURL is sending:

curl -v "https://api.example.com/search?q=test&debug=true"

Handling HTTP Error Codes

# Fail on HTTP error codes
curl -f "https://api.example.com/search?q=test" || echo "Request failed"

# Include HTTP status in output
curl -w "HTTP Status: %{http_code}\n" "https://api.example.com/search?q=test"

Best Practices

1. Always Quote URLs

Wrap URLs in quotes to prevent shell interpretation:

# Good
curl "https://api.example.com/search?q=hello world"

# Bad (may cause issues)
curl https://api.example.com/search?q=hello world

2. Use URL Encoding

Properly encode special characters:

# Use --data-urlencode for automatic encoding
curl -G --data-urlencode "q=hello & goodbye" "https://api.example.com/search"

3. Handle Rate Limiting

Add delays between requests when necessary:

# Add delay between requests
curl "https://api.example.com/search?q=test1"
sleep 1
curl "https://api.example.com/search?q=test2"

4. Set Appropriate Headers

Include proper headers for your requests:

curl -H "User-Agent: MyBot/1.0" \
     -H "Accept: application/json" \
     "https://api.example.com/search?q=test"

Performance Considerations

For high-volume scraping operations, consider connection reuse and parallel requests:

# Reuse connections
curl --keepalive-time 60 "https://api.example.com/search?q=test1"

# Multiple requests in parallel
curl "https://api.example.com/search?q=test1" & \
curl "https://api.example.com/search?q=test2" & \
wait

Troubleshooting Common Issues

URL Too Long Error

Some servers have URL length limits. Consider using POST requests for large parameter sets:

# If URL is too long, use POST with form data
curl -X POST "https://api.example.com/search" \
     -d "q=very long search query with many parameters" \
     -d "additional=parameter"

Character Encoding Issues

Ensure proper character encoding for international characters:

# Specify character encoding
curl --compressed -H "Accept-Charset: utf-8" \
     "https://api.example.com/search?q=%E6%97%A5%E6%9C%AC%E8%AA%9E"

Understanding how to properly use cURL with query parameters is fundamental for API testing, web scraping, and automation tasks. Whether you're building simple scripts or complex data extraction workflows, mastering these techniques will help you interact effectively with web services and APIs.

For more advanced scenarios involving dynamic content and complex user interactions, you might need to complement cURL with specialized browser automation tools that can handle JavaScript-rendered content and complex authentication flows.

Try WebScraping.AI for Your Web Scraping Needs

Looking for a powerful web scraping solution? WebScraping.AI provides an LLM-powered API that combines Chromium JavaScript rendering with rotating proxies for reliable data extraction.

Key Features:

  • AI-powered extraction: Ask questions about web pages or extract structured data fields
  • JavaScript rendering: Full Chromium browser support for dynamic content
  • Rotating proxies: Datacenter and residential proxies from multiple countries
  • Easy integration: Simple REST API with SDKs for Python, Ruby, PHP, and more
  • Reliable & scalable: Built for developers who need consistent results

Getting Started:

Get page content with AI analysis:

curl "https://api.webscraping.ai/ai/question?url=https://example.com&question=What is the main topic?&api_key=YOUR_API_KEY"

Extract structured data:

curl "https://api.webscraping.ai/ai/fields?url=https://example.com&fields[title]=Page title&fields[price]=Product price&api_key=YOUR_API_KEY"

Try in request builder

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon