Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The structure of http message and the usage of curl

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "http message structure and curl use method". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

http protocol is widely used, and there is no need to say more about the use scenarios.

HTTP is an application layer protocol based on TCP. If you want to implement it yourself, you should first implement the functions of tcp, and then implement the agreed functions according to http protocol. This requirement may sometimes arise, for example, if a reader wants to write an http server, he must understand http protocol.

However, this process, from http packets, to tcp packets (with tcp headers), to ip packets (with ip headers), and its reverse process (unpacking process), has ready-made libraries to implement, readers need to do is how to use it better.

This is not a complicated implementation of http, but just an introduction to the data structure of http requests and responses, and how to send http requests conveniently.

This article introduces the message structure of http requests and responses, and the use of curl.

Let the reader see an intuitive example: in Baidu's search box, type in "helloworld" and enter, then an http request will be initiated, use charles to grab the package, you can see such request and response information:

As for the use of charles, as introduced earlier, readers can pay attention to "Guangzhou Xiaocheng" Weixin Official Accounts and consult the contents of the "Software Foundation" menu item.

From the packet perspective, there is a GET method that returns a status code of 200, and so on.

But this is a mix of request and response information. If you look at it separately, what are the data structures of the request and response?

(1) Request message

http requests data, using the following structure:

{Request line, request header, request body}

Xiao Cheng quotes a diagram on the Internet that depicts this structure:

Referring to the figure above, the request line looks like this:

GET /index.html HTTP/1.1

Or:

POST /xiaocheng/about.html HTTP/1.1

Note that the request line generally does not include the host address, which is stored in the request header.

Then there is the request header, divided into multiple lines, each line is a key value pair (key: value format), for example, the request header is like this:

Finally, the request body, generally to GET method is not with the request body, because all the content is placed in the request line (especially url) or request header, generally POST method needs to have the request body, such as iTunes download an APP, POST request data is like this:

(2) Response message

The structure of response data can also be divided into three parts:

{Response line, response header, response body}

Here is an example of a response:

The response line, also known as the status line, contains the http version, status code, and status description.

The response header, like the request header, consists of a key-value line.

The response body is generally the data that the requester wants, but the response body does not necessarily exist. For example, HEAD does not return the response body when requesting the method (in this case, the response line is what the requester wants).

http request and response data, as well as some other knowledge, such as the difference between GET and POST, what does it mean to return 404/403/303, etc., readers of these knowledge can specifically search for it, and the small process is not expanded here.

After a brief introduction to the structure of http request and response messages, the applet introduces a tool that can initiate http requests. This tool is curl.

3) Use of Curl

The small process uses macos, mac comes with curl tool.

Curl is also powerful, providing a range of options. Here are just a few general ways to use it.

get request vs. save

curl www.baidu.com

curl "http://172.17.21.197:54321? pa1=hello"

//save, download

curl -o file.html www.baidu.com

curl -o i.jpg http://img3.douban.com/lpic/s4549903.jpg

// -A copy ie and running platform

curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" www.baidu.com

// -e Copy the entry link (i.e. referrer, used for chain theft)

curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -e "www.google.com" www.baidu.com

// -x Use proxy

curl -x a.b.c.d:54321 http://google.com

// -D Save cookie to file

curl www.baidu.com -o aa.txt -D cookie.txt

// -b reads files and requests them as cookie data

curl www.baidu.com -b cookie.txt

// -H Set request header, where cookies and ua are set

curl -H "Cookie: install_id=14621691626; sid_guard=\"0eb878191489e4247f643672c932c814|1504782359|2592000|Sat\054 07-Oct-2017 11:05:59 GMT\"; \n User-Agent: xxx/1.5.6 (iPhone; iOS 10.2; Scale/2.00)" "https://xxx.yy.com/…"

post

// -d Request body

curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi

curl -d "key=jet" 192.168.2.32:54321

post, in form (multipart/form-data)

// "file" is the key name, starting at random, followed by the file path (including the file package)

curl -F "file=@./ myfile.ok" "http://172.17.xx.xx:54321"

"http message structure and curl usage" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report