tencent cloud

All product documents
Tencent Cloud Observability Platform
Last updated: 2025-03-10 22:14:18
HTTP
Last updated: 2025-03-10 22:14:18

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 Basic Authentication

// 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');
};

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon