nGrinder中快速编写groovy脚本04-发送POST请求

POST请求分类:
1、根据是否修改代码,分为两种方式:
  • 一种是在UI界面添加后自动生成脚本,一种是直接在脚本中添加
2、根据请求参数的不同,主要可以分为两种:
  • param为key value格式
  • body为json格式

一、通过UI方式发送POST请求–key/value参数

  • 通过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置
  • 当选择了请求方法为POST后,在高级配置中默认会在headers中显示Content-Type为x-www-form-urlencoded,同时,添加key/value格式的params:
    在这里插入图片描述
    生成代码如下(由于篇幅限制,去掉import部分):
@RunWith(GrinderRunner)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestRunner {
   

	public static GTest test
	public static HTTPRequest request
	public static NVPair[] headers = []
	public static NVPair[] params = []
	public static Cookie[] cookies = []

	@BeforeProcess
	public static void beforeProcess() {
   
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test = new GTest(1, "www.baidu.com")
		request = new HTTPRequest()
		// Set header datas
		List<NVPair> headerList = new ArrayList<NVPair>()
		headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
		headerList.add(new NVPair("Connection", "keep-alive"))
		headers =<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值