Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,37 @@ chai.request('http://localhost:8080')

#### Setting up requests

Once a request is created with a given VERB, it can have headers, form data,
json, or even file attachments added to it, all with a simple API:
Once a request is created with a given VERB (get, post, etc), you chain on these additional methods to create your request:

| Method | Purpose |
|---|---|
| `.set(key, value)` | Set request headers |
| `.send(data)` | Set request data (default type is JSON) |
| `.type(dataType)` | Change the type of the data sent from the `.send()` method (xml, form, etc) |
| `.attach(field, file, attachment)` | Attach a file |
| `.auth(username, password)` | Add auth headers for Basic Authentication |
| `.query(parmasObject)` | Chain on some GET parameters |

Examples:

`.set()`
```js
// Set a request header
chai.request(app)
.put('/user/me')
.set('Content-Type', 'application/json')
.send({ password: '123', confirmPassword: '123' })
```

`.send()`
```js
// Send some JSON
chai.request(app)
.put('/user/me')
.set('X-API-Key', 'foobar')
.send({ password: '123', confirmPassword: '123' })
```

`.type()`
```js
// Send some Form Data
chai.request(app)
Expand All @@ -113,20 +133,23 @@ chai.request(app)
})
```

`.attach()`
```js
// Attach a file
chai.request(app)
.post('/user/avatar')
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png')
```

`.auth()`
```js
// Authenticate with Basic authentication
chai.request(app)
.get('/protected')
.auth('user', 'pass')
```

`.query()`
```js
// Chain some GET query parameters
chai.request(app)
Expand Down