ExpressJSHTTP方法

/ / ExpressJSHTTP方法

请求中提供了HTTP方法,该方法指定了客户端已请求的操作。下表列出了最常用的HTTP方法-

S.No.Method & Remark
1

GET

GET方法用于获取数据

2

POST

POST方法用于提交数据

3

PUT

PUT方法用于修改数据

4

DELETE

DELETE方法用于删除数据

GET Method

GET请求通过在请求的URL部分中指定参数来从Web服务器检索数据。这是用于文档检索的主要方法。下面的示例利用GET方法获取hello.htm:

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

针对上述GET请求的服务器响应如下:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

HEAD Method

HEAD方法在函数上与GET相似,不同之处在于服务器使用响应行和标头(但没有实体主体)进行回复。下面的示例使用HEAD方法来获取有关hello.htm的头信息:

HEAD /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

针对上述GET请求的服务器响应如下:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed

您会注意到,这里的服务器在标头之后不发送任何数据。

POST Method

当您要将一些数据发送到服务器时使用POST方法,例如文件更新,表单数据等。下面的示例利用POST方法将表单数据发送到服务器,它将由服务器处理。 process.cgi,最后将返回响应:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Content-Type: text/xml; charset=utf-8
Content-Length: 88
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://clearforest.com/">string</string>

服务器端脚本process.cgi处理传递的数据并发送以下响应:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Request Processed Successfully</h1>
</body>
</html>

PUT Method

PUT方法用于请求服务器在给定URL指定的位置存储包含的实体。以下示例请求服务器将给定的实体正文保存在服务器根目录下的hello.htm中:

PUT /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.learnfk.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 182
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

服务器会将给定的实体正文存储在hello.htm文件中,并将以下响应发送回客户端:

HTTP/1.1 201 Created
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>The file was created.</h1>
</body>
</html>

DELETE Method

DELETE方法用于请求服务器在给定URL指定的位置删除文件。以下示例要求服务器删除服务器根目录下的给定文件hello.htm:

DELETE/hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.Learnfkpoint.com
Accept-Language: en-us
Connection: Keep-Alive

服务器将删除提到的文件hello.htm,并将以下响应发送回客户端:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>URL deleted.</h1>
</body>
</html>

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

AI重塑测试开发系统实践 -〔陈磊〕

学透Spring:从入门到项目实战 -〔丁雪丰〕

如何讲好一堂课 -〔薛雨〕

物联网开发实战 -〔郭朝斌〕

如何看懂一幅画 -〔罗桂霞〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

RPC实战与核心原理 -〔何小锋〕

Android开发高手课 -〔张绍文〕

持续交付36讲 -〔王潇俊〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者