24

HTTP 定义了八种方法(有时称为“动词”)

你能帮我找到每个例子,以便我更好地测试和理解它们吗?

4

2 回答 2

58

首先,您应该查看HTTP 1.1 规范,尤其是部分方法定义

  • OPTIONS 获取有关服务器如何允许与之通信的信息。

    要求:

    OPTIONS * HTTP/1.1
    Host: example.com
    

    回复:

    HTTP/1.1 200 OK
    Date: …
    Allow: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE
    Content-Length: 0
    
  • GET 检索资源。

    要求:

    GET /foo/bar HTTP/1.1
    Host: example.com
    

    回复:

    HTTP/1.1 200 OK
    Date: …
    Content-Type: text/html;charset=utf-8
    Content-Length: 12345
     
    <!DOCTYPE …
    
  • HEADGET类似,但仅返回 HTTP 标头。

    要求:

    HEAD /foo/bar HTTP/1.1
    Host: example.com
    

    回复:

    HTTP/1.1 200 OK
    Date: …
    Content-Type: text/html;charset=utf-8
    Content-Length: 12345
    
  • POST 创建一个新资源。

    要求:

    POST /foo/bar HTTP/1.1
    Host: example.com
    Content-Type: application/x-www-form-urlencoded
     
    action=addentry&subject=Hello,%20World
    

    回复:

    HTTP/1.1 201 Created
    Date: …
    Content-Length: 0
    Location: http://example.com/foo/bar        
    
  • PUT 向服务器发送数据。

  • DELETE 删除现有资源。

  • TRACE 返回客户端发送的请求头。

    要求:

    TRACE /foo/bar HTTP/1.1
    Host: example.com
    

    回复:

    HTTP/1.1 200 OK
    Date: …
    Content-Length: 17
     
    Host: example.com
    

我不知道这些例子是否正确。随时纠正它们。

于 2009-04-19T15:51:36.973 回答
13

您可以使用cURL命令行工具试验不同的 HTTP 方法。例如:

curl --head http://www.google.co.uk

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Sun, 19 Apr 2009 15:33:24 GMT
Expires: -1
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=a2a414b9a84c8ffd:TM=1240155204:LM=1240155204:S=16kZnqzeSxIJT3jv; expires=Tue, 19-Apr-2011 15:33:24 GMT; path=/; domain=.google.co.uk
Server: gws
Transfer-Encoding: chunked
  • -X 选项允许您指定除 GET 之外的 HTTP 方法。
于 2009-04-19T15:34:52.567 回答