HTTP traffic
Sending requests
HTTP sends a request from the page, Send API Request from the context. Which one to use, and how each sends bodies, forms and files.
Which one
HTTPsends the request from the page, with the browser'sfetch. Use it to test what the page itself can do: CORS, its cookies, route rules andWait For *all apply.Send API RequestNew in Browser 21.1.0 sends it from the context, outside any page. Use it to set up or check data next to the UI, with the session the page logged in with. It needs no page.
See Page traffic and API traffic for everything that differs.
Arguments
| `HTTP` | `Send API Request` | |
|---|---|---|
| Body | body | data |
| Form, URL-encoded | form | form |
| Form with files | multipart | multipart |
| Query | in url | params, or in url |
| Timeout | timeout, the whole response with its body | timeout, the same |
| Error status | never fails | fails with failOnStatusCode=True |
| Redirects | redirect | maxRedirects |
| Other options | those of fetch: credentials, mode, cache … | maxRetries, ignoreHTTPSErrors |
| Returns | HttpResponse: the headers the page may read | APIResponse: all headers, set-cookie too |
An option you leave out keeps the default of fetch or of Playwright.
Bodies
Give at most one of the body, form and multipart. An empty form or
multipart is sent too, as fetch does: an empty URL-encoded body, or a multipart
body without parts. Values follow the
shared rules. Only text gets a different
Content-Type:
| Body | `HTTP` | `Send API Request` |
|---|---|---|
| Text that is JSON | application/json | application/octet-stream |
| Other text | text/plain;charset=UTF-8 | application/octet-stream |
bytes | none | application/octet-stream |
A Content-Type in headers wins. In Send API Request, application/json also
turns text that is not JSON into a JSON string: hello is sent as "hello".
HTTP sends GET and HEAD without a body. Text, JSON, form and multipart are
left out; bytes fail, so binary data is never dropped unnoticed.
Forms and files
form is a dictionary of text fields. multipart adds files, each a dictionary with
path or buffer:
See MultipartFields for the keys.
- The body and the
bufferfiles can be at most 4 MiB together. Apathfile does not count. - In
HTTP, do not set a Content-Type formultipart: the browser then leaves out the boundary, and the server cannot read the body.
Failures and logging
HTTPfails whenfetchfails: a network error, a request CORS blocks,redirect=erroron a redirect, a wrongintegrity.Send API Requestfails on a network error, a timeout, too many redirects and, withfailOnStatusCode, an error status.- Error messages leave out Playwright's call log, because it lists header values.
- The response is logged without its body, and the body at
TRACElevel.Send API Requestlogs the response headers only atDEBUGlevel, because they can hold cookies.