Skip to content

Commit

Permalink
Merge fcbd208 into 7f9a8a5
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorewahle committed Aug 19, 2019
2 parents 7f9a8a5 + fcbd208 commit e560421
Showing 1 changed file with 26 additions and 3 deletions.
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

0 comments on commit e560421

Please sign in to comment.