本文你可以了解到
- 预请求脚本输入界面
- 设置 Url
- 设置Request Header
- 设置Request 查询参数
- 设置文本Request Body
- 设置 Json Request Body
- 设置Form-Data Request Body
- 访问变量
RestBird不仅支持通过图形界面进行API测试,还支持脚本编程。你可以通过Pre-request, ReponseValidation and PureScript提供强大的编程能力,撰写脚本,完成各种复杂的任务。
预请求脚本输入界面
如下图,点击“Pre-Request Scripts”(预请求脚本)标签页。在下面的输入框输入脚本。在进行API请求时,会先执行预请求脚本。
设置Url
执行下面脚本,会把你请求的URL地址改为下面示例脚本的URL。
# encoding: utf-8 import requests def PreRequest(request, ctx) : request.url = "http://www.google.com"
设置Request Header
# encoding: utf-8 import requests def PreRequest(request, ctx) : request.headers['aa'] = 'bb' request.headers['counter'] = '100'
设置请求查询参数
如示例代码,发送请求时,会把预请求key、value作为请求参数:http://xxxxx.com/?key2=value2&key1=value1
# encoding: utf-8 import requests # GET http://xxxxx.com/?key2=value2&key1=value1 def PreRequest(request, ctx) : allparams = {'key1': 'value1', 'key2': 'value2'} request.params = allparams
设置文本Request Body
# encoding: utf-8 import requests def PreRequest(request, ctx) : request.data = "hello world"
设置Json Request Body
# encoding: utf-8 import requests import json def PreRequest(request, ctx) : payload ={'key1':'value1', 'key2':'value2'} request.data = json.dumps(payload)
设置Form-Data Request Body
# encoding: utf-8 import requests def PreRequest(request, ctx) : payload = {'key1': 'value1', 'key2': 'value2'} request.data = payload
访问变量
# encoding: utf-8 import requests def PreRequest(request, ctx) : request.method = ctx.getGlobal('method') request.url = ctx.vars.get('url', "http://www.google.com/")
来源:https://blog.csdn.net/qq_40352059/article/details/99458707