Bash Arrays by Examples: A Beginner's Guide

πŸ‘€ Views 12.5K
πŸ”„ Shares 847
⏰ Read time 7 min

πŸ” Prerequisites

Before diving into sending HTTP requests, ensure you have a solid understanding of basic Bash scripting concepts such as variables, loops, conditionals, and operators. Additionally, familiarity with HTTP protocols and common request methods like GET, POST, PUT, and DELETE will help you better understand how to interact with web services.


πŸ› οΈ Tools for Sending HTTP Requests in Bash

Bash offers several command-line tools for making HTTP requests, each with its own strengths and use cases. Below are some of the most commonly used tools, along with examples and best practices for their usage.

1. Using curl – The Swiss Army Knife of HTTP Requests

The curl command is one of the most versatile and widely used tools for transferring data with URLs. It supports numerous protocols, including HTTP, HTTPS, FTP, and more, making it a go-to choice for sending HTTP requests in Bash scripts.

πŸ”Ή Example: Sending a GET Request

curl "https://jsonplaceholder.typicode.com/todos/1"

This command retrieves the JSON data from the specified URL and prints it to the terminal.

Sample Output (from JSONPlaceholder):

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

πŸ”Ή Example: Sending a POST Request with JSON Data

curl -X POST "https://jsonplaceholder.typicode.com/todos" \
-H "Content-Type: application/json" \
-d '{"title": "John Doe", "completed": false}'

This sends a POST request to create a new todo item with a JSON body containing the title and completion status.

πŸ’‘ Tip: Use man curl to explore additional options and flags for customizing your requests.


2. Using wget – Simplified File Downloading

While wget is primarily used for downloading files, it can also be used to retrieve web content via HTTP GET requests. It’s particularly useful for batch downloading files or mirroring websites.

πŸ”Ή Example: Downloading a File

wget "https://jsonplaceholder.typicode.com/todos"

This downloads the file and saves it as index.html by default.

πŸ”Ή Example: Saving to a Custom File Name

wget -O todos.json "https://jsonplaceholder.typicode.com/todos"

The -O flag allows you to specify a custom filename for the downloaded content.


3. Using nc (Netcat) – Raw HTTP Requests

Netcat (nc) allows you to send raw TCP packets, making it a useful tool for crafting and sending custom HTTP requests.

πŸ”Ή Example: Sending a GET Request

echo -e "GET /todos/1 HTTP/1.1\nHost: jsonplaceholder.typicode.com\n\n" | nc jsonplaceholder.typicode.com 80

This opens a TCP connection to the server on port 80 and sends an HTTP GET request manually.


4. Using telnet – Manual HTTP Interaction

Similar to nc, telnet can be used to send raw HTTP requests, though it may not be installed by default on all systems.

πŸ”Ή Example: Sending a GET Request

telnet jsonplaceholder.typicode.com 80 <<EOF
GET /todos/1 HTTP/1.1
Host: jsonplaceholder.typicode.com

EOF

This sends a raw HTTP request to fetch a todo item from the JSONPlaceholder API.


5. Using openssl – Secure HTTP Requests (HTTPS)

When working with HTTPS endpoints, openssl can be used to establish a secure connection and send HTTP requests over SSL/TLS.

πŸ”Ή Example: Sending an HTTPS GET Request

echo -e "GET /todos/1 HTTP/1.1\nHost: jsonplaceholder.typicode.com\n\n" | openssl s_client -connect jsonplaceholder.typicode.com:443

This initiates a secure connection and sends an HTTP GET request to the server.


6. Using socat – Advanced Networking Tool

socat is a multipurpose relay tool that can be used to send HTTP requests over TCP connections.

πŸ”Ή Example: Sending a GET Request

echo -e "GET /todos/1 HTTP/1.1\nHost: jsonplaceholder.typicode.com\n\n" | socat - TCP:jsonplaceholder.typicode.com:80

This sends a raw HTTP request to the server using the socat utility.


7. Using lynx – Text-Based Browser

Lynx is a lightweight, text-based browser that can be used to retrieve and display web content in the terminal.

πŸ”Ή Example: Fetching JSON Data

lynx -dump https://jsonplaceholder.typicode.com/todos/1

This outputs the raw JSON data in the terminal window.


8. Using httpie – Human-Friendly HTTP Client

If installed, httpie offers a more intuitive and readable way to send HTTP requests compared to raw curl commands.

πŸ”Ή Example: Sending a GET Request

http GET https://jsonplaceholder.typicode.com/todos/1

πŸ”Ή Example: Sending a POST Request

http POST https://jsonplaceholder.typicode.com/todos title="John Doe" completed:=false

This sends a POST request with a JSON body containing the title and completion status.


πŸ›‘οΈ Best Practices for HTTP Requests in Bash Scripts

  • βœ… Use the correct HTTP method (GET, POST, PUT, DELETE) for your task.
  • βœ… Always set appropriate headers (e.g., Content-Type, Authorization).
  • βœ… Validate user input before sending requests to avoid errors or security issues.
  • βœ… Handle errors gracefully using exit codes and conditional checks.
  • βœ… Prefer curl or wget for most HTTP tasks due to their reliability and wide support.
  • βœ… Document your scripts clearly to make them maintainable and reusable.

πŸ“ˆ Conclusion

Bash scripting provides a wide array of tools for sending HTTP requests, from the versatile curl and wget to lower-level tools like nc, telnet, and openssl. By mastering these tools and following best practices, you can automate API interactions, manage web services, and streamline your development workflow effectively.

Whether you’re building automation scripts, debugging APIs, or integrating with external services, understanding how to send HTTP requests in Bash is a valuable skill that will enhance your productivity and system management capabilities.

Remember to always test your scripts in a safe environment and choose the right tool for the job based on your specific use case and requirements.


πŸ“š Resources & Further Reading

bash script bash http request bash curl wget get post put delete headers body json openssl

Related Articles

Bash Arrays by Examples: A Beginner's Guide

Bash Arrays by Examples: A Beginner's Guide

Arrays in Bash can store strings, numbers, or a combination of both. They provide a convenient way to group related data together and manipulate it as a whole.

bash array
Bash Arrays by Examples: A Beginner's Guide

Bash Arrays by Examples: A Beginner's Guide

Learn how to craft and execute HTTP requests directly from your Bash scripts, with detailed explanations, code examples, and best practices for various scenarios.

bash script bash http request bash curl wget
Load more articles