我想使用vegeta测试一些POST API,但是post负载没有正确发送。
vegeta命令:
vegeta attack -targets=tmp -rate=1 -duration=1s | tee results.bin | vegeta reporttmp文件:
POST http://server-ip/api/salon
@saloninfo.jsonsaloninfo.json文件:
{
"salon_id" : "562737c1ff567dbd5574c814"
}基本上,有效载荷是空的{}。
有人能帮我查一下我可能错过了什么吗。
发布于 2017-08-15 09:24:56
我相信这样做是有好处的:
POST http://server-ip/api/salon
Content-Type: application/json
@saloninfo.json发布于 2020-11-11 11:57:55
这里提到了另一种方式。如果您熟悉golang,可能会很有用。
import (
"os"
"time"
"net/http"
vegeta "github.com/tsenart/vegeta/lib"
)
func customTargeter() vegeta.Targeter {
return func(tgt *vegeta.Target) error {
if tgt == nil {
return vegeta.ErrNilTarget
}
tgt.Method = "POST"
tgt.URL = "http://server-ip/api/salon" // your url here
payload := `{
"salon_id" : "562737c1ff567dbd5574c814"
}` // you can make this salon_id dynamic too, using random or uuid
tgt.Body = []byte(payload)
header := http.Header{}
header.Add("Accept", "application/json")
header.Add("Content-Type", "application/json")
tgt.Header = header
return nil
}
}
func main() {
rate := vegeta.Rate{Freq: 1, Per: time.Second} // change the rate here
duration := 1 * time.Minute // change the duration here
targeter := customTargeter()
attacker := vegeta.NewAttacker()
var metrics vegeta.Metrics
for res := range attacker.Attack(targeter, rate, duration, "Whatever name") {
metrics.Add(res)
}
metrics.Close()
reporter := vegeta.NewTextReporter(&metrics)
reporter(os.Stdout)
}我在这里所做的是,为攻击者对象提供速率、持续时间和目标函数。使用以下方法运行脚本:
go run name_of_the_file.goNewTextReporter用于在终端本身中打印结果。在vegeta库中还有其他类型的记者可用。根据需要使用它们。
发布于 2016-12-05 11:45:35
我相信这是因为你需要设置content type: application/json。
不幸的是,文档和github问题间接地提到了它,但是没有确切地指出它应该在哪里,无论是作为json的头部,还是在像Curl这样的vegeta命令中。还在这里找答案。
https://stackoverflow.com/questions/38102510
复制相似问题