Skip to main content

Demos and runnable examples

Here is a collection of runnable HTTP examples.

The archive demos.zip also contains sample httt-client-env.json and js script files.

1. basic.http
### Simple GET request

GET https://httpbin.org/get HTTP/1.1

### GET with query parameters

GET https://httpbin.org/get?name=kulala&age=25 HTTP/1.1

### POST with JSON body

POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{
"name": "John Doe",
"email": "john@example.com"
}

### PUT with headers

PUT https://httpbin.org/put HTTP/1.1
Content-Type: application/json
X-Custom-Header: test-value

{
"id": 1,
"status": "updated"
}

### DELETE request

DELETE https://httpbin.org/delete HTTP/1.1

### GET with multiple headers

GET https://httpbin.org/headers HTTP/1.1
Accept: application/json
User-Agent: My-Test-Client
Authorization: Bearer test-token
2. variables.http
###

# The order of the environment variables resolution is as follows:
# - System environment variables
# - http-client.env.json file
# - .env file


### Basic GET request

GET https://api.thecatapi.com/v1/images/search HTTP/1.1

@BASE_URL = https://api.thecatapi.com/v1
@API_KEY = your_api_key_here

### GET request with variables and request parameters

GET {{BASE_URL}}/images/search?limit=3 HTTP/1.1

### Request with environment variables

GET {{FROM_ENV_FILE_URL}}/breeds HTTP/1.1
3. access-token.http
###

# https://neovim.getkulala.net/docs/usage/dynamically-setting-environment-variables-based-on-response-json
# <request-name>.response.body.<json.path>


### Get token (simulating login)

# @name login
POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{
"username": "demo",
"password": "demo123",
"access_token": "asdfasdfasdfasdf"
}

### Access protected resource with Bearer token

GET https://httpbin.org/bearer HTTP/1.1
Authorization: Bearer {{login.response.body.json.access_token}}

### Access protected resource with custom token header

GET https://httpbin.org/headers HTTP/1.1
X-API-Key: {{login.response.body.json.username}}
4. cookie.http
### Set cookies

# @name setCookies
GET https://httpbin.org/cookies/set?sample=cookie123&test=cookie456 HTTP/1.1

### Call protected resource with cookies

GET https://httpbin.org/cookies HTTP/1.1
Cookie: sample={{setCookies.response.cookies.sample.value}}; test={{setCookies.response.cookies.test.value}}
5. script.http
### Get token (simulating login)

< {%
// Pre-request script
const crypto = require('crypto');
const timestamp = Date.now();
const nonce = crypto.randomBytes(8).toString('hex');

// Set request-scoped variables
request.variables.set('TIMESTAMP', timestamp);
request.variables.set('NONCE', nonce);

console.log('Pre-request variables set:', { timestamp, nonce });
%}
# @name login
POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{
"username": "demo",
"password": "demo123",
"timestamp": "{{TIMESTAMP}}",
"nonce": "{{NONCE}}"
}

> {%
// Post-request script
const token = response.body.json.username + '_' + response.body.json.nonce;

// Set global variable for use in subsequent requests
client.global.set('AUTH_TOKEN', token);

console.log('Received response and set AUTH_TOKEN:', token);
%}

###

# @name REQUEST_ONE
POST https://httpbin.org/post HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{AUTH_TOKEN}}

{
"token": "SOME_TOKEN",
"deep": {
"nested": [
{
"key": "foo"
}
]
}
}

> {%
var fs = require('fs');
fs.writeFileSync('TOKEN.txt', "Demo with bilibili");
client.global.set('GORILLA_TOKEN', response.body.json.token);
console.log('Received response and set GORILLA_TOKEN:', response.body.json.token);
%}

###

# @name REQUEST_TWO
POST https://httpbin.org/post HTTP/1.1
Accept: application/json
Content-Type: application/json

{
"gorilla": "{{GORILLA_TOKEN}}"
}

> ./post-request.js

###

# @name REQUEST_THREE
POST https://httpbin.org/post HTTP/1.1
Accept: application/json
Content-Type: application/json

{
"date_header_from_request_two": "{{BONOBO}}"
}
6. body-from-file.http
POST https://httpbin.org/post
Content-Type: application/json

< ./body-from-file.json
7. testing-and-reporting.http
# Testing and Reporting Demo

# Define environment variables
@BASE_URL = https://httpbin.org
@OCCUPATION = Developer

### POST request with JSON body
POST {{BASE_URL}}/post HTTP/1.1
Accept: application/json
Content-Type: application/json

{
"data": {
"name": "John Doe",
"occupation": "{{OCCUPATION}}",
"age": 30
}
}

> {%
let json = response.body.json.data;

client.test("Test suite 1", function() {
assert(json.name == "John Doe", "Check if name is correct")
assert.same(json.occupation, "Developer", "Check occupation")
client.assert.true(json.age == 30, "Age is correct")
client.assert.false(json.age == 40, "Age is not 40")
});

client.log("Test Script Output 1")

client.test("Test suite 2", function() {
assert.hasString(json.occupation, "Develop", "Check if occupation contains 'Develop'")
assert.responseHas('responseCode', 200, "Check if response code is 200")
assert.headersHas('Content-Type', "application/json", "Check content type")
});

assert.jsonHas("json.data.occupation", "Developer", "Check json payload")
%}