乐闻世界logo
搜索文章和话题

How to send GET and POST requests using cURL?

3月7日 19:41

In cURL, GET and POST requests are the two most commonly used HTTP methods, each with different use cases and parameter configurations.

GET Request

GET requests are used to retrieve data, with parameters typically appended to the URL.

bash
# Basic GET request curl https://api.example.com/users # With query parameters curl "https://api.example.com/users?page=1&limit=10" # With request headers curl -H "Authorization: Bearer token123" \ -H "Accept: application/json" \ https://api.example.com/users # Show response headers curl -i https://api.example.com/users # Show only response headers curl -I https://api.example.com/users

POST Request

POST requests are used to submit data, requiring -X POST to specify the method and -d to pass data.

bash
# Send JSON data curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"John","email":"john@example.com"}' # Send form data curl -X POST https://api.example.com/login \ -d "username=admin&password=123456" # Read data from file curl -X POST https://api.example.com/upload \ -H "Content-Type: application/json" \ -d @data.json # Upload file curl -X POST https://api.example.com/upload \ -F "file=@/path/to/file.pdf"

Key Differences

FeatureGETPOST
Data LocationURL parametersRequest body
Data SizeLimited by URL lengthUnlimited
SecurityParameters visibleRelatively secure
CachingCacheableNot cached
IdempotencyIdempotentNon-idempotent

Common Parameters

  • -X or --request: Specify HTTP method
  • -H or --header: Add request header
  • -d or --data: Send data
  • -F or --form: Upload form/file
  • -i: Show response headers and body
  • -I: Show only response headers
标签:cURL