@auth directive.
Prerequisites
- hitspec installed (Installation)
- Familiarity with the Basic CRUD example
The Complete Test File
Create a file calledauth-flow.http:
auth-flow.http
Step-by-Step Breakdown
Login and capture the token
The first request simulates a login and captures credentials from the response.The
>>>capture block extracts values from the response body. These become available as {{login.token}} and {{login.userId}} in later requests.In a real API, you would capture from a path like
body.access_token or body.data.token. This example uses httpbin.org which echoes back the posted JSON.Use the token with @auth bearer
The Key points:But
@auth bearer directive automatically adds an Authorization: Bearer <token> header.# @depends loginensures this runs after the login request# @auth bearer {{login.token}}injects the captured token- httpbin.org’s
/bearerendpoint validates the Bearer token and returnsauthenticated: true
@auth bearer is cleaner and makes the intent explicit.Chain multiple authenticated requests
Multiple requests can depend on the same login and reuse the token.The
startsWith operator checks that the Authorization header was sent correctly.Dependent chain: login -> getProfile -> updateProfile
You can create multi-level dependency chains.
updateProfile depends on getProfile, which depends on login. hitspec resolves the full dependency graph automatically.Basic authentication
hitspec supports multiple auth types. Here is Basic auth:The
@auth basic directive base64-encodes the credentials and sends them as Authorization: Basic <encoded>.Supported Auth Types
hitspec supports eight authentication methods. Here are the most common:| Type | Syntax | Header Sent |
|---|---|---|
| Bearer | @auth bearer {{token}} | Authorization: Bearer <token> |
| Basic | @auth basic user, pass | Authorization: Basic <base64> |
| API Key (header) | @auth apiKey X-API-Key, {{key}} | X-API-Key: <key> |
| API Key (query) | @auth apiKeyQuery api_key, {{key}} | ?api_key=<key> |
| Digest | @auth digest user, pass | Digest authentication |
| AWS Sig v4 | @auth aws {{access}}, {{secret}}, {{region}}, {{service}} | AWS Signature v4 |