Interface IAPIRequestContext
- Namespace
- Microsoft.Playwright
- Assembly
- Microsoft.Playwright.dll
This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare environment or the service to your e2e test.
Each Playwright browser context has associated with it IAPIRequestContext instance which shares cookie storage with the browser context and can be accessed via APIRequest or APIRequest. It is also possible to create a new APIRequestContext instance manually by calling NewContextAsync(APIRequestNewContextOptions?).
**Cookie management**
IAPIRequestContext returned by APIRequest
and APIRequest shares cookie storage with the corresponding
IBrowserContext. Each API request will have Cookie
header populated
with the values from the browser context. If the API response contains Set-Cookie
header it will automatically update IBrowserContext cookies and requests
made from the page will pick them up. This means that if you log in using this API,
your e2e test will be logged in and vice versa.
If you want API requests to not interfere with the browser cookies you should create
a new IAPIRequestContext by calling NewContextAsync(APIRequestNewContextOptions?).
Such APIRequestContext
object will have its own isolated cookie storage.
public interface IAPIRequestContext : IAsyncDisposable
Methods
CreateFormData()
Creates a new IFormData instance which is used for providing form and multipart data when making HTTP requests.
IFormData CreateFormData()
Returns
DeleteAsync(string, APIRequestContextOptions?)
Sends HTTP(S) DELETE request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
Task<IAPIResponse> DeleteAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
FetchAsync(IRequest, APIRequestContextOptions?)
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
**Usage**
JSON objects can be passed directly to the request:
var data = new Dictionary<string, object>() {
{ "title", "Book Title" },
{ "body", "John Doe" }
};
await Request.FetchAsync("https://example.com/api/createBook", new() { Method = "post", DataObject = data });
The common way to send file(s) in the body of a request is to upload them as form
fields with multipart/form-data
encoding. Use IFormData to
construct request body and pass it to the request as multipart
parameter:
var file = new FilePayload()
{
Name = "f.js",
MimeType = "text/javascript",
Buffer = System.Text.Encoding.UTF8.GetBytes("console.log(2022);")
};
var multipart = Context.APIRequest.CreateFormData();
multipart.Set("fileField", file);
await Request.FetchAsync("https://example.com/api/uploadScript", new() { Method = "post", Multipart = multipart });
Task<IAPIResponse> FetchAsync(IRequest urlOrRequest, APIRequestContextOptions? options = null)
Parameters
urlOrRequest
IRequestTarget URL or Request to get all parameters from.
options
APIRequestContextOptionsCall options
Returns
FetchAsync(string, APIRequestContextOptions?)
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
**Usage**
JSON objects can be passed directly to the request:
var data = new Dictionary<string, object>() {
{ "title", "Book Title" },
{ "body", "John Doe" }
};
await Request.FetchAsync("https://example.com/api/createBook", new() { Method = "post", DataObject = data });
The common way to send file(s) in the body of a request is to upload them as form
fields with multipart/form-data
encoding. Use IFormData to
construct request body and pass it to the request as multipart
parameter:
var file = new FilePayload()
{
Name = "f.js",
MimeType = "text/javascript",
Buffer = System.Text.Encoding.UTF8.GetBytes("console.log(2022);")
};
var multipart = Context.APIRequest.CreateFormData();
multipart.Set("fileField", file);
await Request.FetchAsync("https://example.com/api/uploadScript", new() { Method = "post", Multipart = multipart });
Task<IAPIResponse> FetchAsync(string urlOrRequest, APIRequestContextOptions? options = null)
Parameters
urlOrRequest
stringTarget URL or Request to get all parameters from.
options
APIRequestContextOptionsCall options
Returns
GetAsync(string, APIRequestContextOptions?)
Sends HTTP(S) GET request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
**Usage**
Request parameters can be configured with params
option, they will be serialized
into the URL search parameters:
var queryParams = new Dictionary<string, object>()
{
{ "isbn", "1234" },
{ "page", 23 },
};
await request.GetAsync("https://example.com/api/getText", new() { Params = queryParams });
Task<IAPIResponse> GetAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
HeadAsync(string, APIRequestContextOptions?)
Sends HTTP(S) HEAD request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
Task<IAPIResponse> HeadAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
PatchAsync(string, APIRequestContextOptions?)
Sends HTTP(S) PATCH request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
Task<IAPIResponse> PatchAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
PostAsync(string, APIRequestContextOptions?)
Sends HTTP(S) POST request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
**Usage**
JSON objects can be passed directly to the request:
var data = new Dictionary<string, object>() {
{ "firstName", "John" },
{ "lastName", "Doe" }
};
await request.PostAsync("https://example.com/api/createBook", new() { DataObject = data });
To send form data to the server use form
option. Its value will be encoded
into the request body with application/x-www-form-urlencoded
encoding (see
below how to use multipart/form-data
form encoding to send files):
var formData = Context.APIRequest.CreateFormData();
formData.Set("title", "Book Title");
formData.Set("body", "John Doe");
await request.PostAsync("https://example.com/api/findBook", new() { Form = formData });
The common way to send file(s) in the body of a request is to upload them as form
fields with multipart/form-data
encoding. Use IFormData to
construct request body and pass it to the request as multipart
parameter:
var file = new FilePayload()
{
Name = "f.js",
MimeType = "text/javascript",
Buffer = System.Text.Encoding.UTF8.GetBytes("console.log(2022);")
};
var multipart = Context.APIRequest.CreateFormData();
multipart.Set("fileField", file);
await request.PostAsync("https://example.com/api/uploadScript", new() { Multipart = multipart });
Task<IAPIResponse> PostAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
PutAsync(string, APIRequestContextOptions?)
Sends HTTP(S) PUT request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
Task<IAPIResponse> PutAsync(string url, APIRequestContextOptions? options = null)
Parameters
url
stringTarget URL.
options
APIRequestContextOptionsCall options
Returns
StorageStateAsync(APIRequestContextStorageStateOptions?)
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
Task<string> StorageStateAsync(APIRequestContextStorageStateOptions? options = null)
Parameters
options
APIRequestContextStorageStateOptionsCall options