Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Sending requests with dynamic body #330

Closed
AVoskoboinikov opened this issue Aug 28, 2018 · 8 comments
Closed

[QUESTION] Sending requests with dynamic body #330

AVoskoboinikov opened this issue Aug 28, 2018 · 8 comments

Comments

@AVoskoboinikov
Copy link

AVoskoboinikov commented Aug 28, 2018

Question

Hi @tsenart

I have a question about request body.
Lets say I have API to create an entity in db and the request body looks like

{
    entityName: "burger"
    entityId: 42
}

Now I want to test that API and send 100 requests. The problem is that field entityId has to be unique.
So, my question is - is there any way to generate unique value for field before sending request to API endpoint?

@AVoskoboinikov AVoskoboinikov changed the title Sending requests with dynamic body [QUESTION] Sending requests with dynamic body Aug 28, 2018
@tsenart
Copy link
Owner

tsenart commented Aug 28, 2018

There sure is. Use JSON target format which allows you to set the request body of each target. Paired with the jq tool, it should be fairly easy to do what you need.

@tsenart
Copy link
Owner

tsenart commented Aug 28, 2018

Here's a specific incantation for you:

jq -ncM 'while(true; .+1) | {method: "POST", url: "http://localhost:6060", body: {entityName: "burger", entityId: .} | @base64}'  | \
vegeta attack -lazy --format=json -duration=30s | tee results.bin | vegeta report

@AVoskoboinikov
Copy link
Author

Thanks @tsenart
And is there any way to do similar trick when using Vegeta as lib without cli?

@tsenart
Copy link
Owner

tsenart commented Aug 30, 2018

Yes. A vegeta.Targeteris just a Plain Old Function. Hence, you can define whatever you want in there.

tr := func(id uint64) vegeta.Targeter {
    type entity struct {
        Name string `json:"entityName"`
        ID   uint64 `json:"entityId"`
    }
    return func(t *Target) (err error) {
        t.Method = "POST"
        t.URL    = "http://localhost:6060"

        t.Body, err = json.Marshal(&entity{
            Name: "burger",
            ID:    atomic.AddUint64(&id, 1),
        })

        return err
    }
}(0)

@AVoskoboinikov
Copy link
Author

Great. Thanks, that works for me!

@akashdas2019
Copy link

@base64}'  

I have a big json and hence I cannot add the post request and body and header here.
I need to pass that via target.txt file. So in that case how to modify the command so that the order_number field in that is unique everytime.

@tsenart
Copy link
Owner

tsenart commented Apr 6, 2019

@akashdas2019: Better write a script or little program that does that.

@aryahmph
Copy link

Yes. A vegeta.Targeteris just a Plain Old Function. Hence, you can define whatever you want in there.

tr := func(id uint64) vegeta.Targeter {
    type entity struct {
        Name string `json:"entityName"`
        ID   uint64 `json:"entityId"`
    }
    return func(t *Target) (err error) {
        t.Method = "POST"
        t.URL    = "http://localhost:6060"

        t.Body, err = json.Marshal(&entity{
            Name: "burger",
            ID:    atomic.AddUint64(&id, 1),
        })

        return err
    }
}(0)

How about doing with 2 requests ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants