# withHeaders

Specifies request headers. A request header is an HTTP header that can be used in an HTTP request to provide information about the request context

# Syntax

withHeaders(name, value) // key-value pair
withHeaders(headers)     // key-value pairs as object
1
2

# Usage

# โœ… Correct Usage

// single key value pair
await spec()
  .get('/api/users')
  .withHeaders('Content-Type', 'application/json')
  .expectStatus(200);
1
2
3
4
5
// multiple key value pair
await spec()
  .get('/api/users')
  .withHeaders('Content-Type', 'application/json')
  .withHeaders('Accept', 'application/json')
  .expectStatus(200);
1
2
3
4
5
6
// key value pair object
await spec()
  .get('/api/users')
  .withHeaders({
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  })
  .expectStatus(200);
1
2
3
4
5
6
7
8

# โ— Incorrect Usage

// don't use both overridden methods in same spec to introduce confusion
await spec()
  .get('/api/users')
  .withHeaders('Content-Type', 'application/json')
  .withHeaders({
    'Accept': 'application/json'
  })
  .expectStatus(200);
1
2
3
4
5
6
7
8

# Arguments

# > name (string)

Name of the header key.

# > value (string)

Value of the header.

# > headers (object)

key-value pairs of headers.

# Examples

# Single header

const { spec } = require('pactum');

await spec()
  .get('https://httpbin.org/bearer')
  .withHeaders('Authorization', 'Bearer abc')
  .expectStatus(200);
1
2
3
4
5
6

# Multiple headers

const { spec } = require('pactum');

await spec()
  .get('https://httpbin.org/bearer')
  .withHeaders('Authorization', 'Bearer abc')
  .withHeaders('Accept', 'application/json')
  .expectStatus(200);

// or

await spec()
  .get('https://httpbin.org/bearer')
  .withHeaders({
    'Authorization': 'Bearer abc',
    'Accept': 'application/json'
  })
  .expectStatus(200);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# See Also