A dedicated and fun sharing series of Shell tips 🌝

preface

How do I make an HTTP request from the command line? When you think about it, believing in this requirement scenario is actually quite common in our daily work (programmers, of course). For example, you need to test whether the API provided by the server is working properly. Of course, there are such as want to upload or download a file through HTTP service; Cross-end file transfer; Interface testing: enable static resource services, and so on.

So today, explore and share the command line operations that toss HTTP services, requests, proxies, and more.

Of course, some students might say you can use a browser, Postman, etc. Here is not to deny the convenience of graphics application software, but the command line also has its advantages, more simple and convenient, close to the bottom, at the same time rich expression and so on.



First, to experiment, we need an HTTP server. This article usesGithub.com/typicode/js…Quickly set up a REST API server. (Refer to the README of the warehouse for the building process, and the building will be successful within 30 seconds)





Preparation is that simple. Begin the text.

cURL

First is to introduce the cURL, pre-installed on the general system. The main function of this tool is to upload and download URL specified data. Here’s an excerpt of its name:

It displays the data (by default), the user can see the URL (see), and see can be shortened to a single letter C. CURL is a URL client. The letter “C” also stands for “client”.

Curl URL Request Library. Like PHP (PHP: Hypertext Preprocessor), it’s also interesting. CURL is a powerful tool. There’s a whole book out there about cURL, and there are tons of articles on the Internet about it. So, of course, I’ll just cover common operations that are relevant to the purpose of this article. CURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL.

Download resources

The -o, –remote-name option represents writing the response output to a local file with the same file name as the one on the remote service.

The curl -o 127.0.0.1:3000 / postsCopy the code

The result is shown in the picture below. A progress indicator will be displayed on the terminal to show the user the progress details.





To be more flexible, use the -o, –output option to write output to a file instead of the standard output stream (stdout)

The curl -o posts 127.0.0.1:3000 / postsCopy the code

This is equivalent to using the curl standard output stream to redirect the output to any file

The curl 127.0.0.1:3000 / posts > postsCopy the code

If you need to download multiple resources at the same time, it is also easy to write them together, as follows:

Curl \ -o file1 127.0.0.1:3000/posts \ -o file2 127.0.0.1:3000/commentsCopy the code



Curl also has many other downloadable features, such as limiting download speed and size, retry after failure, setting an offset to continue downloading, and specifying the download range. You can explore it if you’re interested.

Detailed mode and silent mode

If you want to display detailed information about the request response, add the -v, –verbose option. It is generally used for debugging.

The curl -v 127.0.0.1:3000 / postsCopy the code

As shown in the figure below, it can be divided into the following areas.

    • The first few lines show that cURL attempted and successfully connected to the IP address (127.0.0.1:3000).
  1. The first few lines represent an HTTP request header with no body.

  2. The first few lines of < represent the corresponding HTTP response header and the corresponding resource.
  3. The last line indicates that the HTTP connection is closed.






If you only care about HTTP response headers, you can use the -i, –include option.

The curl -i 127.0.0.1:3000 / postsCopy the code

The corresponding mode is -s, –silent. To do this, curl downloads resources without printing any progress information.

The curl - sO 127.0.0.1:3000 / postsCopy the code

request

CURL is a GET request by default. So how do YOU use POST, HEAD, PUT requests? Curl does not specify methods on the command line. Curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl

POST

If -d, –data, or -f, –form arguments are used, the POST request is used by default. The following is an example:

Curl -v 127.0.0.1:3000/posts -d title=fake-server -d author=sulircCopy the code

Note the request header: POST /posts HTTP/1.1 proves that cURL does use a POST request.





Of course, if you want to write simple, you can use the browser “URL encoding” form to send, as follows:

The curl -v 127.0.0.1:3000 / posts - d'title=express-server&author=david'
Copy the code

Alternatively, if the number of parameters is very large, you can put them in a file and read the parameters from the file as follows:

The curl -v 127.0.0.1:3000 / posts - d @ data - the fileCopy the code

Again, -i corresponds to the HEAD method. -t corresponds to the PUT method. I won’t go into that.

Set the request header

Content-type: Application/X-www-form-urlencoded So if we want to request the server in JSON format. Then you need the ability to set the request header with the following arguments: -h, –header.

curl -v -d '{"title":"mock-server","author":"christina"}' -H 'Content-Type: application/json'127.0.0.1:3000 / postsCopy the code

The following is an example:





CURL also has a wealth of options for requests. If you need to, you can read the manual for a general understanding.



Note that modern browsers such as Chrome, Safari and Firefox, as well as proxy tools such as Charles, provide a convenient option to copy requests to cURL.

HTTP Cookie

Due to the stateless nature (or lack of it) of HTTP, HTTP cookies were invented to maintain state between requests. CURL doesn’t use cookies by default. So how do you use it?



As we know, cookie-based technologies are primarily used by browsers. The browser manages Cookies in the Application.





In order to facilitate the experiment, a SIMPLE Koa server is set up and displays two cookies. The code is abbreviated as follows:

const createId = () = > Math.random().toString(16).slice(2).toUpperCase();

app.use(async (ctx) => {
  if (ctx.get("cookie")) {
    ctx.body = ctx.get("cookie");
  } else {
    ctx.cookies.set("track-id", createId());
    ctx.cookies.set("token"."#secret token#");
    ctx.body = "no-cookie"; }}); app.listen(PORT,() = > {
  console.log(`My server running, listening port ${PORT}`);
});
Copy the code

Cookies

On the command line, Cookies need to be written to a file. The parameters are -c, –cookie-jar

Curl -v -c cookie. TXT 127.0.0.1:3000Copy the code

The following is an example:





Note that the client (in this case, the terminal) does not have local Cookies when first accessed. From the request header. After running the command, the set-cookie field appears prominently in the response header, as highlighted in blue in the figure above. A cookie. TXT file is also available. Curl uses a Netscape cookie format that works with browsers.



The use of Cookies

So the next time a request, can pass – b, – cookies < data | filename > parameters Settings.

Curl -v -b cookie. TXT 127.0.0.1:3000Copy the code

The following is an example:



The blue highlighted area in the figure indicates that the Cookies have been correctly read and carried from the request header to the server. Finally, you can also see that the Cookies have been echoed in the response data.

Destroying session Cookies

Typically in browsers, all session Cookies are destroyed when you restart. So how do you simulate the same behavior in cURL? CURL starts a new session with the -j, –junk-session-cookies argument.

Curl - v-j-b cookie. TXT 127.0.0.1:3000Copy the code

The diagram below:



As you can see, cURL did not select to send even though cookie. TXT was present, and the server sent a new set-cookie response header indicating that the client had updated the Cookies (track-ID has been updated).

HTTPie

Warehouse: github.com/httpie/http…

Httpie is an extremely useful tool, which is described as follows:

HTTPie (pronounced aitch-tee-tee-pie) is a command-line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers. The http & https commands allow for creating and sending arbitrary HTTP requests. They use simple and natural syntax and provide formatted and colorized output.

In short, we can understand it as a friendlier alternative to cURL. Httpie can also override some of the common cURL operations, but the display is much friendlier and more readable.



For a simple LOCALhost GET request, we can omit the IP as shown in the following example:



The terminal output is the colored HTTP response header, formatted response data. The contrast with cURL really shines.

Download resources

How do I download resources? Redirection will definitely work.

http :3000/posts > posts.json
Copy the code

If you want to specify to download to a file. You can use the –output, -o options.

http :3000/posts -o my_posts.json
Copy the code

Neither command prints any information. If you want to know the progress and the response header. You can use the –download, -d arguments.

http :3000/posts -d -o my_posts.json
Copy the code

The following is an example:



Detailed mode and silent mode

Httpie generally displays only the response header if you also want to display request details.

http -v :3000/posts
Copy the code

Notice the following separation of empty lines in the request header and response header (see HTTP protocol)



If you want silent mode, use the –quiet option. All standard output streams and standard error streams will then be redirected to /dev/null. -d -o does not affect the above options, that is, the file will still be downloaded normally.

request

The use of request methods in Httpie is very clear. By default, the GET method is also used. If you need to specify other methods, the HTTP method is placed directly in front of the URL.

POST

So, for example, using the POST method, you can use the following:

http -v POST :3000/posts title=mock-server author=ygj
Copy the code

Here’s an example:



Of course, the same is true for PUT and DELETE requests, but since they are rarely used in general work, the examples will not be shown.



CURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL: cURL By default, content-type: application/json is used for httpie POST requests with data parameters.



If you want to transmit data using Content-Type: application/x-www-form-urlencoded, you can use the -f, –form parameter.

http -v -f POST :3000/posts title=mock-server author=ygj
Copy the code

The following is an example:





Like cURL, you can use the @ symbol to reference files when you need to upload complex data. As follows:

http -v POST :3000/posts @posts.json
Copy the code

The following is an example:

Set the request header

Of course, as a client, when making an HTTP request, you must have the ability to customize the HTTP request header. It’s also very simple in Httpie. For example, if I wanted to customize a request header, it would be natural to write:

http -v :3000/profile 'X-Demo-Time: 2021/01/03'
Copy the code

Figure:



Single quotes are written to prevent the shell from automatically expanding. If you are sure that the written request header will not risk expansion, you can leave the quotation marks alone. For example, this mode does not require:

http -v :3000/profile X-Demo-Author:sulirc
Copy the code

HTTP Cookie

The Cookie processing in cURL is described above. The same operation is available in HTTPie. The Koa server from above is reused here for an example.



When we first requested it, the expected display was as follows:



The use of Cookies

HTTPie provides a way to write HTTP headers to Cookies. Manually copy multiple Cookies, note that; Space:

http -v :3000 'Cookie:track-id=CC649CF69E3E9; token=#secret token#; '
Copy the code

The following is an example:



But how inconvenient manual management is!



If I need to keep a session cookie to recycle the request. HTTPie also provides a dedicated Sessions management scheme.

Write named Session

You can write session Cookies to a local session.json file with the –session option.

http -v --session=./session.json :3000
Copy the code

Json files are stored in the following format:





We can also name sessions more specifically.

http -v --session=david-session.json :3000
Copy the code


The following legend shows that two different session Cookies are enabled and stored.

Use Session

It is also very intuitive to use, and is consistent with writing named sessions. Represents to reuse this Session.

http -v --session=david-session.json :3000
Copy the code





There is also the creation of anonymous, read-only sessions. I’m not going to go into the details here. You can check the official documentation.

Configuration rules

Httpie: Httpie: httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie: Httpie:



See the official documentation for details on Httpie:httpie.io/docs. In addition to the basic features described above, there are proxy, SSL, offline mode features to explore (if necessary).



The obvious disadvantage of cURL is that it does not come with the system itself and requires additional installation. Unlike cURL, it is not a tool that engineers are familiar with and a de facto standard. CURL is not as powerful as cURL, but it is a useful tool to use in your daily work. It is also worth trying to use HTTP command line to improve your work experience.

http-server

Warehouse: github.com/http-party/…

Httpie and cURL. I love this simple and useful one-click way to build a server. It’s as simple as typing the http-server command in the current file directory (default port 8080) :

http-server ./
Copy the code

The following is an example:



Of course, some students will ask, what is the use of building a static resource server? Simply put, you build the ability to connect your local file system to the Internet. More down-to-earth, though, is the ability to quickly share files from one’s computer with colleagues or mobile phones via HTTP, especially useful in certain scenarios.



Http-server also has some other parameters, such as setting SSL, port, cross-domain, etc., which you can refer to yourself if necessary.

For simple file transfer functions, you are advised to use the encrypted, cross-platform, and multi-file transfer CLI tool croc: github.com/schollz/cro… .

summary

Bash/Shell is actually something you can learn for more than a decade. The beauty of bash/ Shell is that it is one of those things where the more things you know, the more magic you can put together, and it is also a lower-level tool. The process of learning and exploring bash/shell is also the process of learning the operating system. I hope you will enjoy this post and that it will be helpful in your daily work. Above, thanks for reading.

The resources