本篇主要介紹在http請求post傳參的場景,有了以前GET相關的介紹,相信POST的使用你們也能快速的掌握。POST的請求處理和GET請求最大的區別無非是參數的傳遞方式,因此在Gatling腳本里主要是去設置http body Content-Type。json
本篇主要介紹json和form表單兩種參數提交方式的處理:
JSON方式app
import io.gatling.core.Predef._ import io.gatling.core.scenario.Simulation import io.gatling.http.Predef._ class JsonSimulation extends Simulation { val httpConf = http.baseURL("http://127.0.0.1:7001/tst") //注意這裏,設置提交內容type val headers_json = Map("Content-Type" -> "application/json") val scn = scenario("json scenario") .exec(http("test_json") //http 請求name .post("/order/get") //post url .headers(headers_json) //設置body數據格式 //將json參數用StringBody包起,並做爲參數傳遞給function body() .body(StringBody("{\"orderNo\":201519828113}")).asJSON) setUp(scn.inject(atOnceUsers(10))).protocols(httpConf) }
Form方式post
import io.gatling.core.Predef._ import io.gatling.http.Predef._ class FormSimulation extends Simulation { val httpConf = http .baseURL("http://computer-database.gatling.io") //注意這裏,設置提交內容type val contentType = Map("Content-Type" -> "application/x-www-form-urlencoded") //聲明scenario val scn = scenario("form Scenario") .exec(http("form_test") //http 請求name .post("/computers") //post地址, 真正發起的地址會拼上上面的baseUrl http://computer-database.gatling.io/computers .headers(contentType) .formParam("name", "Beautiful Computer") //form 表單的property name = name, value=Beautiful Computer .formParam("introduced", "2012-05-30") .formParam("discontinued", "") .formParam("company", "37")) setUp(scn.inject(atOnceUsers(1)).protocols(httpConf)) }
若是須要動態參數,可參考GET請求動態參數的處理。測試
到這裏Gatling的介紹基本就結束了,Gatling的測試腳本自己就不復雜,主要面向http請求的測試。咱們從官方下載的包裏有很是多的例子適合各類場景,咱們在實際使用中通常會對其進行拷貝,而後修改爲本身須要的,因此測試過程當中你們也不要有什麼負擔。url