tencent cloud

云压测

产品概述
购买指南
计费概述
按量计费(后付费)
购买方式
欠费说明
退费说明
快速入门
操作指南
简单模式压测
脚本模式压测
JMeter 模式压测
管理项目
管理场景
流量录制
环境管理
定时压测
压测报告
访问控制
告警管理
标签管理
错误代码手册
实践教程
使用 Prometheus 观测性能压测指标
使用云压测回放 GoReplay 录制的请求
API 文档
History
Introduction
API Category
Making API Requests
PTS-related APIs
Data Types
Error Codes
JavaScript API 列表
JavaScript API 列表概述
pts/global
pts/http
pts
pts/dataset
pts/grpc
pts/jsonpath
pts/protobuf
pts/redis
pts/sql
pts/url
pts/util
pts/ws
pts/socketio
pts/socket
常见问题
相关协议
服务等级协议
使用限制
隐私政策
数据处理和安全协议

HTTP

PDF
聚焦模式
字号
最后更新时间: 2025-03-10 16:46:52

HTTP Get

// Send a http get request
import http from 'pts/http';
import { check, sleep } from 'pts';

export default function () {
// simple get request
const resp1 = http.get('http://httpbin.org/get');
console.log(resp1.body);
// if resp1.body is a json string, resp1.json() transfer json format body to a json object
console.log(resp1.json());
check('status is 200', () => resp1.statusCode === 200);

// sleep 1 second
sleep(1);

// get request with headers and parameters
const resp2 = http.get('http://httpbin.org/get', {
headers: {
'Connection': 'keep-alive',
'User-Agent': 'pts-engine'
},
query: {
'name1': 'value1',
'name2': 'value2',
}
});

console.log(resp2.json().args.name1); // 'value1'
check('body.args.name1 equals value1', () => resp2.json().args.name1 === 'value1');
};

HTTP Post (raw)

// Send a post request
import http from 'pts/http';
import { check } from 'pts';

export default function () {
const resp = http.post(
'http://mockhttpbin.pts.svc.cluster.local/post',
{
user_id: '12345',
},
{
headers: {
'Content-Type': 'application/json',
},
}
);
console.log(resp.json().json.user_id); // 12345
check('body.json.user_id equals 12345', () => resp.json().json.user_id === '12345', resp);
}

HTTP Post (x-www-form-urlencoded)

// Send a form urlencoded request
import http from 'pts/http';
import { check } from 'pts';

export default function () {
const resp = http.post(
'http://mockhttpbin.pts.svc.cluster.local/post',
{
user_id: '12345',
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);

console.log(resp.json().headers['Content-Type']); // application/x-www-form-urlencoded
console.log(resp.json().form.user_id); // 12345
check('body.form.user_id equals 12345', () => resp.json().form.user_id === '12345', resp);
}

HTTP Post (form-data)

// Send a multipart request
import http from 'pts/http';
import {check} from 'pts';

const fileData = open("./sample/tmp.js")

export default function () {
const formData = new http.FormData();
formData.append('data', 'some data');
formData.append('file', http.file(fileData));
const resp = http.post('http://httpbin.org/post', formData.body(), {
headers: {
'Content-Type': formData.contentType()
}
});

console.log(resp.json().headers['Content-Type']); // multipart/form-data; boundary=c7fced8e5c0ce8939a96691da77d13f635c389cdbcc951e8784114edb41f
console.log(resp.json().form.data); // some data
console.log(resp.json().files.file.length); // 801
check('body.form.data equals some data', () => resp.json().form.data === 'some data');
};

HTTP 基础鉴权

// Http basic authentication
import http from 'pts/http';
import {check} from 'pts';

export default function () {
const user = 'user';
const passwd = 'passwd';
const resp = http.get(`http://${user}:${passwd}@httpbin.org/basic-auth/user/passwd`);

console.log(resp.json().authenticated); // true
check('body.authenticated equals true', () => resp.json().authenticated === true);
}
// Send a request with cookie
import http from 'pts/http';
import {check} from 'pts'

export default function () {
const resp = http.get('http://httpbin.org/cookies', {
headers: {
cookie: 'k=v'
}
});

console.log(resp.json().cookies.k); // v
check('body.cookies.k equals v', () => resp.json().cookies.k === 'v');
};


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈