HTTP entry (a): curl view request and response in Bash

HTTP entry (a): curl view request and response in Bash

HTTP

  • This paper briefly summarizes the requests and responses of HTTP .
  • The main purpose of this paper is to summarize and learning content for future reference.
  • Principles and detailed tutorial can refer to HTTP document (MDN).
  • This article belongs Ma Taotao all.
  • Cited herein pictures and text belongs to original author, tort deleted.
  • If wrong, please point out in the comments area below, welcomed the positive discussions.

Interaction with the server browser


Alternately

  • Browser is responsible for initiating the request
  • The server receiving a request port 80
  • Server is responsible for the content returned ( response )
  • Responsible browser downloads the response content

HTTP protocol role is to guide the browser and the server how to communicate .

request


Use curl -s -v -H "Mtt: xxx" -- https://www.baidu.comto create a request Bash in and get a response.

Command line Interpretation
theme: : brief information : indicating a request, a response indicating : Add a response header Explanation
-s
-v > <
-H

Theme request and response: requesting content:
Request and response screenshot

> GET / HTTP/1.1                GET方法发送请求,获取默认根目录,使用http1.1协议
> Host: www.baidu.com           主机
> User-Agent: curl/7.58.0       用curl7.58这个东西发送的请求
> Accept: */*                   接受服务器返回的任何内容
> Frank: xxx
>                               有一个回车

Different test parameters

curl -X POST -s -v -H "Frank: xxx" -- "https://www.baidu.com"Test
content is requested

POST / HTTP/1.1    因为-x改成了post
Host: www.baidu.com 
User-Agent: curl/7.54.0
Accept: */*
Frank: xxx

curl -X POST -d "1234567890" -s -v -H "Frank: xxx" -- https://www.baidu.comTest
content is requested

POST / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.54.0
Accept: */*
Frank: xxx
Content-Length: 10   内容长度为是10
Content-Type: application/x-www-form-urlencoded

1234567890


"-d 1234567890": I want to upload data to Baidu 1234567890

Content-Length: 10: Content length is 10
Content-Type: I want to upload content format

GetIt is to obtain, but only browser content, do not do other things
postpublished, uploaded mean. For example, say I want to register log on, put the data written on the inside -d, and then when the request directly uploaded.

Summary: The requested format

1 动词 路径(HTTP规定必须以/开头) 协议/版本
2 Key1: value1
2 Key2: value2
2 Key3: value3
2 Content-Type: application/x-www-form-urlencoded
2 Host: www.baidu.com
2 User-Agent: curl/7.54.0
3 (永远是回车,用来区分二四部分)
4 要上传的数据
  1. Up request includes four parts, a minimum of three parts. (That is the fourth part can be empty)
  2. 第三部分永远都是一个回车(n)
  3. 动词有 GET POST PUT(整体更新) PATCH(部分更新) DELETE(删除一些东西) HEAD OPTIONS 等
  4. 这里的路径包括「查询参数」,但不包括「锚点」(服务器不看锚点,只看路径)
  5. 如果你没有写路径,那么路径默认为 /
  6. 第 2 部分中的 Content-Type 标注了第 4 部分的格式

响应


上面三个请求示例,前两个请求对应的响应分别为

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2443
Content-Type: text/html(百度返回的时候百度的数据长度和内容的格式)
Date: Tue, 10 Oct 2017 09:14:05 GMT
Etag: "5886041d-98b"
Last-Modified: Mon, 23 Jan 2017 13:24:45 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

<!DOCTYPE html>
<!--STATUS OK--><html> <head> 后面太长,省略了……


HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Tue, 10 Oct 2017 09:19:47 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 后面太长,省略了……
  1. GET 请求和 POST 请求对应的响应可以一样,也可以不一样
  2. 响应的第四部分可以很长

响应的格式

1 协议/版本号 状态码 状态解释
2 Key1: value1
2 Key2: value2
2 Content-Length: 17931
2 Content-Type: text/html
3
4 要下载的内容

• status code is said to server browser, then details see Wikipedia HTTP status code
• 1xx unusual
• 2xx indicates success 200 success (get) 204 created successfully (POST)
• 3xx roll bar represents 301 (formerly of people move out up , where will move to the second part of the location of a key: value in the complaint you) 302 (out of a few days will come back means the server temporary absence )
• 4xx Ah that you are wrong (certainly visitors error for example, you entered the wrong address)
• 5xx represent Well, I was wrong (expressed server wrong a)
• Content-Type part 2 of the label format part 4
Content-Type • part 2 of follow MIME specifications

Guess you like

Origin www.cnblogs.com/baimeishaoxia/p/11965122.html