Prerequisites
- hitspec installed (Installation)
- A terminal
The Complete Test File
Here is the full test file you will build step by step. Create a file calledcrud.http:
crud.http
Step-by-Step Breakdown
Define variables
The Variables reduce duplication and make it easy to switch environments. You can override them with a
@baseUrl variable at the top of the file stores the API base URL. Reference it anywhere with {{baseUrl}}..hitspec.env.json file or the --env flag.List all posts (GET collection)
The first request fetches all posts and validates the response shape.Key concepts:
###separates requests within a file# @name listPostsgives the request an identifier for captures and dependencies# @tags smoke, readlets you filter with--tags smoke- The
>>> ... <<<block contains assertions expect body type arraychecks the response is a JSON arrayexpect body[0].id existschecks nested fields using JSON path syntax
Get a single post (GET by ID)
This request fetches a specific post and adds a response time assertion.
expect body.id == 1checks an exact valueexpect body.userId type numbervalidates the JSON typeexpect duration < 2000fails if the response takes more than 2 seconds
Create a post (POST with JSON body)
This request creates a new resource and captures its ID for later use.The
>>>capture ... <<< block extracts body.id from the response and stores it as newPostId. Later requests reference it as {{createPost.newPostId}} (request name dot capture name).Update the post (PUT with dependency)
This request depends on
createPost and uses the captured ID.# @depends createPost ensures this request runs after createPost completes, even in parallel mode.Delete the post (DELETE)
The final request deletes the resource. It depends on
updatePost to preserve execution order.Key Concepts Used
| Concept | Syntax | Purpose |
|---|---|---|
| Variables | @baseUrl = ... | Reusable values |
| Request names | # @name createPost | Identify requests for captures and dependencies |
| Tags | # @tags smoke, read | Filter which requests to run |
| Assertions | expect status 200 | Validate responses |
| Captures | newPostId from body.id | Extract values from responses |
| Dependencies | # @depends createPost | Control execution order |