Linux:ab 发送 POST、PUT 请求


ab 可以发送 GET、HEAD、POST、PUT 请求,但是不支持模拟 DELETE 等 Method 的请求。

https://stackoverflow.com/questions/10601821/apache-bench-how-to-force-put-delete-and-post-method

From the manual page:
-i Do HEAD requests instead of GET.
-p POST-file File containing data to POST. Remember to also set -T.
-u PUT-file File containing data to PUT. Remember to also set -T.
But no DELETE, et al.

使用 golang 实现简版服务端,Dump Request:

package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
)

func main() {
	http.HandleFunc("/", handle)
	http.ListenAndServe(":1280", nil)
}

func handle(w http.ResponseWriter, r *http.Request) {
	req, err := httputil.DumpRequest(r, true)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s\n", req)
}

执行命令,启动服务端:

$ go run main.go

GET(默认)
$ ab -n 1 -c 1 http://127.0.0.1:1280/students?id=123456
GET /students?id=123456 HTTP/1.0
Host: 127.0.0.1:1280
Connection: close
Accept: */*
User-Agent: ApacheBench/2.3

POST
-p POST-file
File containing data to POST. Remember to also set -T.
$ ab -p POST.json -T application/json -n 1 -c 1 http://127.0.0.1:1280/students
POST /students HTTP/1.0
Host: 127.0.0.1:1280
Connection: close
Accept: */*
Content-Length: 73
Content-Type: application/json
User-Agent: ApacheBench/2.3

{
	"id": "123456",
	"age": 25,
	"name": "test1280",
	"addr": "BeiJing"
}

其中:

$ cat POST.json 
{
	"id": "123456",
	"age": 25,
	"name": "test1280",
	"addr": "BeiJing"
}

PUT
-u PUT-file
File containing data to PUT. Remember to also set -T.
$ ab -u PUT.json -T application/json -n 1 -c 1 http://127.0.0.1:1280/students
PUT /students HTTP/1.0
Host: 127.0.0.1:1280
Connection: close
Accept: */*
Content-Length: 72
Content-Type: application/json
User-Agent: ApacheBench/2.3

{
	"id": "123456",
	"age": 25,
	"name": "test1280",
	"addr": "ShanXi"
}

其中:

$ cat PUT.json 
{
	"id": "123456",
	"age": 25,
	"name": "test1280",
	"addr": "ShanXi"
}

HEAD
-i     Do HEAD requests instead of GET.
$ ab -i -n 1 -c 1 http://127.0.0.1:1280/
HEAD / HTTP/1.0
Host: 127.0.0.1:1280
Connection: close
Accept: */*
User-Agent: ApacheBench/2.3
Logo

更多推荐