10

I want to test a few POST APIs using vegeta, but the post payload is not getting send properly.

vegeta command:

vegeta attack -targets=tmp -rate=1 -duration=1s | tee results.bin | vegeta report

tmp file:

POST http://server-ip/api/salon
@saloninfo.json

saloninfo.json file:

{
  "salon_id" : "562737c1ff567dbd5574c814"
}

Basically, the payload is going empty {}.

Can someone please check, what i might be missing.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Kushal Singh
  • 101
  • 1
  • 4

3 Answers3

9

I believe that this should do the trick:

POST http://server-ip/api/salon
Content-Type: application/json
@saloninfo.json
beatcracker
  • 6,714
  • 1
  • 18
  • 41
3

Mentioning an alternative way here. May be useful if you are familiar with 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)
}

what I am doing here is, providing the Attacker object with rate, duration and a targeter function. Run the script using:

go run name_of_the_file.go

NewTextReporter is used to print the results in the terminal itself. There are other kinds of reporters available in the vegeta library. Use them as per requirement.

Amrullah Zunzunia
  • 1,617
  • 16
  • 31
  • 1
    This approach is very convenient/useful if i want to dynamically generate different payloads. – fritz Nov 20 '20 at 11:37
2

I believe this is because you need to set the content type: application/json.

Unfortunately the documentation and github issues mention it obliquely but with no indication as to exactly where it's supposed to be, either as a header for the json or in the vegeta command like Curl. Still looking for the answer here.

csmallon
  • 63
  • 7