Linux:ab 发送 POST、PUT 请求

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值