Skip to main content

Variables

Variables#

one can define variables in http file via {{var}}. define it once and use it everywhere

example 1:#

//test.http
GET https://httpbin.org/{{var=get}}
? path = "{{var}}"
// curl -X GET 'https://httpbin.org/get?path=get'

try in browser here

python -m dothttp test.http

In the above httpdef, var is set to get. one can overwrite it via

python -m dothttp test.http --properties var=post

producing

curl -X GET 'https://httpbin.org/post?path=post'

example 2:#

//test.http
POST https://httpbin.org/post
json({
"firstname": "{{name=john}}",
"lastname": "{{lastname=doe}}",
"location": "hyderabad",
// "location": "delhi",
"full name": "{{name}} {{lastname}}",
})

try in browser here

curl output will be

curl -X POST \
-H 'Content-Length: 90' \
-H 'Content-Type: application/json' \
-d '{"firstname": "john", "lastname": "doe", "location": "hyderabad", "full name": "john doe"}' \
https://httpbin.org/post

override via properties by passing in command line

python -m dothttp --properties name=shiva lastname=prasanth

curl -X POST \
-H 'Content-Length: 102' \
-H 'Content-Type: application/json' \
-d '{"firstname": "shiva", "lastname": "prasanth", "location": "hyderabad", "full name": "shiva prasanth"}' \
https://httpbin.org/post

Dothttp Environment#

passing properties from command line is not always handy. one can define

.dothttp.json in same directory as http file

//.dothttp.json
{
"*" : {
"default": "variables"
},
"john": {
"name": "jhonny",
"lastname": "depp"
},
// ironman
"ironman": {
"name": "robert",
"lastname": "dny jnr"
}
}

random#

while integration testing, having to replace few fields are pain points. using $randomStr will generate random string of random length.

  • random string ($randomStr)
  • random integer ($randomInt)
  • random float( $randomFloat)
  • random bool ( $randomBool)
//test.http
POST https://httpbin.org/post
json({
"full name": "john {{name=$randomStr:10}}",
"name": "john {{name}}",
"height": {{$randomInt:2}},
})

name --> shiva + "randomString of length 10 chars" height --> randomInteger of length 2

try in browser here

made request will like below

curl -X POST \
-H 'Content-Length: 73' \
-H 'Content-Type: application/json' \
-d '{"full name": "john Z4gi1r8IEX", "name": "john Z4gi1r8IEX", "height": 51}' \
https://httpbin.org/post

uuid#

uuid is completely random string with 36 char length

Example#

POST "https://httpbin.org/post"
json(
{
// generates random uuid
"uuid": "{{$uuid}}"
}
)

randomSlug#

randomSlug or randomLoremSlug

we’ll see random, human-readable slugs (e.g., brave-purple-penguin) in the web development world. These slugs offer the uniqueness of a number ID but can be more playful and fun.

{{$randomSlug:3}} --> create slug with 3 (brave-purple-penguin) {{$randomSlug:2}} --> create slug with 3 (brave-ranga)

example#

POST "https://httpbin.org/post"
json(
{
// creates slug with human readable random
"slug": "{{$randomSlug:10}}"
}
)

timestamp#

While testing, uniquely identifiying request is hard, one can use timestamp (epoch) in headers or queryparams to distinguish from ther requests

usage:

{{$timestamp}} --> generates 1629338487 (current timestamp)

Example:#

POST "https://httpbin.org/post"
json(
{
// creates current timestamp
"timestamp": "{{$timestamp}}"
}
)

System Command#

Environment file variables and properties defined in a file or through the command line are static. However, some variables—such as access_token or sts—are time-sensitive and must be generated dynamically when needed.

System Command Variables are those that can be generated on demand by running a command in the command line.

To define a system command variable, create a key-value pair in the $commands section of your environment file. Here’s an example:

{
"$commands": {
"access_token": "az account get-access-token"
}
}

In this example, the access_token is set up to be generated dynamically by executing the specified command when accessed.

Os Environment Variables:#

To use an OS environment variable in an HTTP file, prefix the variable name with DOTHTTPENV.

Example:#

Define or export a variable, such as DOTHTTP_ENV_date, to use {{date}} it in the following HTTP request:

GET https://req.dothttp.dev/get
json({
"data": "{{date}}"
})

In this example, DOTHTTP_ENV_date can be accessed as {{date}} within the HTTP request.

Math Expression:#

You can use simple math expressions that you may not want to evaluate immediately for debugging purposes. Dothttp handles the expansion and substitution for you.

Example:#

POST https://httpbin.org/post
json(
{
# here instead of using 3600, we use 1*60*60
"secondsInHour": "{{$expr:1*60*60}}",
}
)