Using the lwp-request command (with examples)
The lwp-request command is a simple command-line HTTP client that is built with libwww-perl. It allows you to easily make HTTP requests and interact with web servers from the command line.
In this article, we will explore several different use cases of the lwp-request command, along with code examples and explanations for each use case.
1: Make a simple GET request
Code:
lwp-request -m GET http://example.com/some/path
Motivation: The motivation for making a simple GET request is to retrieve information from a web server. GET requests are the most common type of HTTP request and are used to retrieve resources from a server.
Explanation:
The -m
option specifies the HTTP method to be used, in this case, “GET”. The URL specified after the command is the target URL to send the request to.
Example output:
200 OK
Content-Length: 1024
Content-Type: text/html
<html>
<body>
Hello, World!
</body>
</html>
2: Upload a file with a POST request
Code:
lwp-request -m POST http://example.com/some/path < path/to/file
Motivation: The motivation for uploading a file with a POST request is to send data to a server. POST requests are commonly used for submitting forms and uploading files.
Explanation:
The -m
option specifies the HTTP method to be used, in this case, “POST”. The URL specified after the command is the target URL to send the request to. The <
symbol followed by the file path indicates that the contents of the file will be included in the request body.
Example output:
201 Created
Location: http://example.com/some/path/1234
3: Make a request with a custom user agent
Code:
lwp-request -H 'User-Agent: user_agent' -m METHOD http://example.com/some/path
Motivation: The motivation for making a request with a custom user agent is to mimic a specific web browser or client application. User agents are often used by servers to determine how to format and deliver content to the client.
Explanation:
The -H
option specifies a custom header to be included in the request. In this case, the header being set is “User-Agent” with a value of “user_agent”. The -m
option specifies the HTTP method to be used, and the URL specified after the command is the target URL to send the request to.
Example output:
200 OK
Content-Type: text/html
4: Make a request with HTTP authentication
Code:
lwp-request -C username:password -m METHOD http://example.com/some/path
Motivation: The motivation for making a request with HTTP authentication is to access protected resources on a server. HTTP authentication is commonly used to secure web applications and restrict access to certain areas of a website.
Explanation:
The -C
option specifies the username and password to be used for HTTP authentication. In this case, the format is “username:password”. The -m
option specifies the HTTP method to be used, and the URL specified after the command is the target URL to send the request to.
Example output:
200 OK
Content-Type: text/html
5: Make a request and print request headers
Code:
lwp-request -U -m METHOD http://example.com/some/path
Motivation: The motivation for printing request headers is to inspect the headers being sent to a server. Request headers contain information about the client, such as the user agent and any custom headers that are being sent.
Explanation:
The -U
option tells lwp-request to display the request headers in addition to the response headers and content. The -m
option specifies the HTTP method to be used, and the URL specified after the command is the target URL to send the request to.
Example output:
GET /some/path HTTP/1.1
User-Agent: libwww-perl/6.53
6: Make a request and print response headers and status chain
Code:
lwp-request -E -m METHOD http://example.com/some/path
Motivation: The motivation for printing response headers and status chain is to inspect the headers and status codes returned by the server. Response headers contain metadata about the response, while status codes indicate the result of the request.
Explanation:
The -E
option tells lwp-request to display the response headers and status chain. The -m
option specifies the HTTP method to be used, and the URL specified after the command is the target URL to send the request to.
Example output:
HTTP/1.1 200 OK
Date: Mon, 01 Nov 2021 00:00:00 GMT
Content-Type: text/html
In conclusion, the lwp-request command is a powerful tool for making HTTP requests from the command line. It provides several options for customizing requests and inspecting response headers and status codes. By understanding these different use cases and their respective arguments, you can leverage the lwp-request command to interact with web servers more effectively.