douzhicong5965 2018-11-15 04:11
浏览 240
已采纳

使用自定义攻击者在Vegeta中的POST请求中发送有效载荷

So I've seen several posts saying you're supposed to put the targets in a temp file and the body in a .json file, but I need to send lots of random data to my site, and ideally I don't want to be constantly writing new random data to these files --so I'd like to do it all in one file. If this is just impossible and I have to use multiple files, please let me know.

All I'm trying to do right now is send a POST request to a webpage that is simply a form with 4 inputs: title, number, volume, and year. I have the following code, but right now it's not sending values. It's sending a payload, but one that has no values. Meaning that a key [] and value "" keep getting stored in my map on the backend. Can anyone see the reason it's sending blanks? Could anyone tell me how I should go about fixing it?

package main

import (
    "encoding/json"
    "fmt"
    "time"

    vegeta "github.com/tsenart/vegeta/lib"
)

func NewCustomTargeter() vegeta.Targeter {
    return func(tgt *vegeta.Target) error {
        if tgt == nil {
            return vegeta.ErrNilTarget
        }

        tgt.Method = "POST"

        tgt.URL = "http://localhost:8080/create.html"

        payload := map[string]string{
            "title":  "junk",
            "number": "junk2",
            "volume": "junk3",
            "year":   "junk4",
        }
        body, _ := json.Marshal(payload)
        tgt.Body = []byte(body)
        return nil
    }
}

func main() {
    rate := vegeta.Rate{Freq: 100, Per: 2 * time.Second}
    duration := 10 * time.Second
    targeter := NewCustomTargeter()
    attacker := vegeta.NewAttacker()
    var metrics vegeta.Metrics
    for res := range attacker.Attack(targeter, rate, duration, "Load Test") {
        metrics.Add(res)
    }
    metrics.Close()
    fmt.Printf("%+v  
", metrics)

}
  • 写回答

1条回答 默认 最新

  • dsj83686 2018-11-15 08:46
    关注

    You need to do few stuff to make attacker able to send data in FormData format.

    First, set the Content-type header value to application/x-www-form-urlencoded. You might need to import net/http package.

    header := http.Header{}
    header.Set("Content-type", "application/x-www-form-urlencoded")
    tgt.Header = header
    

    Then, set put the data into url.Values format. Pass the encoded value of it into tgt.Body. You also need to import net/url package.

    form := url.Values{}
    form.Set("title", "junk")
    form.Set("number", "junk2")
    form.Set("volume", "junk3")
    form.Set("year", "junk4")
    
    tgt.Body = []byte(form.Encode())
    

    Additional notes

    On the data preparation, we can also use map literal style.
    But since url.Values is an alias of map[string][]string (not map[string]string),
    might need some adjustment.

    form := url.Values{
        "title":  []string{"junk"},
        "number": []string{"junk2"},
        "volume": []string{"junk3"},
        "year":   []string{"junk4"},
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。