Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions docs/docs/usage/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ You can add cookies to a request using the `AddCookie` method:
request.AddCookie("foo", "bar");
```

The simple two-parameter form defers domain resolution until execution time — the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:

```csharp
request.AddCookie("foo", "bar", "/path", "example.com");
```

RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the `RestResponse.Cookies` properties, which has the `CookieCollection` type.

However, the usage of a default URL segment parameter is questionable as you can just include the parameter value to the base URL of the client. There is, however, a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created by the request when you call `AddCookie`. Still, the container is only used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.
There is a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created automatically at execution time. The four-parameter `AddCookie` overload populates the container immediately, while the two-parameter form stores cookies in `PendingCookies` until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.

If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookes` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.
If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookies` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.

## Request Body

Expand Down
10 changes: 8 additions & 2 deletions docs/versioned_docs/version-v110/usage/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,17 @@ You can add cookies to a request using the `AddCookie` method:
request.AddCookie("foo", "bar");
```

The simple two-parameter form defers domain resolution until execution time — the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:

```csharp
request.AddCookie("foo", "bar", "/path", "example.com");
```

RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the `RestResponse.Cookies` properties, which has the `CookieCollection` type.

However, the usage of a default URL segment parameter is questionable as you can just include the parameter value to the base URL of the client. There is, however, a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created by the request when you call `AddCookie`. Still, the container is only used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.
There is a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created automatically at execution time. The four-parameter `AddCookie` overload populates the container immediately, while the two-parameter form stores cookies in `PendingCookies` until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.

If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookes` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.
If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookies` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.

### Request Body

Expand Down
10 changes: 8 additions & 2 deletions docs/versioned_docs/version-v111/usage/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,17 @@ You can add cookies to a request using the `AddCookie` method:
request.AddCookie("foo", "bar");
```

The simple two-parameter form defers domain resolution until execution time — the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:

```csharp
request.AddCookie("foo", "bar", "/path", "example.com");
```

RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the `RestResponse.Cookies` properties, which has the `CookieCollection` type.

However, the usage of a default URL segment parameter is questionable as you can just include the parameter value to the base URL of the client. There is, however, a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created by the request when you call `AddCookie`. Still, the container is only used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.
There is a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created automatically at execution time. The four-parameter `AddCookie` overload populates the container immediately, while the two-parameter form stores cookies in `PendingCookies` until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.

If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookes` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.
If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookies` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.

## Request Body

Expand Down
10 changes: 8 additions & 2 deletions docs/versioned_docs/version-v112/usage/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ You can add cookies to a request using the `AddCookie` method:
request.AddCookie("foo", "bar");
```

The simple two-parameter form defers domain resolution until execution time — the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:

```csharp
request.AddCookie("foo", "bar", "/path", "example.com");
```

RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the `RestResponse.Cookies` properties, which has the `CookieCollection` type.

However, the usage of a default URL segment parameter is questionable as you can just include the parameter value to the base URL of the client. There is, however, a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created by the request when you call `AddCookie`. Still, the container is only used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.
There is a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created automatically at execution time. The four-parameter `AddCookie` overload populates the container immediately, while the two-parameter form stores cookies in `PendingCookies` until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.

If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookes` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.
If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookies` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.

## Request Body

Expand Down
10 changes: 8 additions & 2 deletions docs/versioned_docs/version-v113/usage/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ You can add cookies to a request using the `AddCookie` method:
request.AddCookie("foo", "bar");
```

The simple two-parameter form defers domain resolution until execution time — the cookie domain is inferred from the request URL. If you need to specify the path and domain explicitly, use the four-parameter overload:

```csharp
request.AddCookie("foo", "bar", "/path", "example.com");
```

RestSharp will add cookies from the request as cookie headers and then extract the matching cookies from the response. You can observe and extract response cookies using the `RestResponse.Cookies` properties, which has the `CookieCollection` type.

However, the usage of a default URL segment parameter is questionable as you can just include the parameter value to the base URL of the client. There is, however, a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created by the request when you call `AddCookie`. Still, the container is only used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.
There is a `CookieContainer` instance on the request level. You can either assign the pre-populated container to `request.CookieContainer`, or let the container be created automatically at execution time. The four-parameter `AddCookie` overload populates the container immediately, while the two-parameter form stores cookies in `PendingCookies` until the request is executed. The container is used to extract all the cookies from it and create cookie headers for the request instead of using the container directly. It's because the cookie container is normally configured on the `HttpClientHandler` level and cookies are shared between requests made by the same client. In most of the cases this behaviour can be harmful.

If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookes` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.
If your use case requires sharing cookies between requests made by the client instance, you can use the client-level `CookieContainer`, which you must provide as the options' property. You can add cookies to the container using the container API. No response cookies, however, would be auto-added to the container, but you can do it in code by getting cookies from the `Cookies` property of the response and adding them to the client-level container available via `IRestClient.Options.CookieContainer` property.

## Request Body

Expand Down
15 changes: 14 additions & 1 deletion src/RestSharp/Request/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public RestRequest(string? resource, Method method = Method.Get) : this() {
public RestRequest(Uri resource, Method method = Method.Get)
: this(resource.IsAbsoluteUri ? resource.AbsoluteUri : resource.OriginalString, method) { }

readonly List<FileParameter> _files = [];
readonly List<FileParameter> _files = [];
readonly List<Cookie> _cookies = [];

/// <summary>
/// Always send a multipart/form-data request - even when no Files are present.
Expand Down Expand Up @@ -263,4 +264,16 @@ public RestRequest RemoveParameter(Parameter parameter) {
}

internal RestRequest AddFile(FileParameter file) => this.With(x => x._files.Add(file));

internal RestRequest AddCookie(Cookie cookie) => this.With(x => x._cookies.Add(cookie));

/// <summary>
/// Cookies added via the 2-param <c>AddCookie(name, value)</c> overload that have not yet been
/// resolved into <see cref="CookieContainer"/>. Domain is inferred from the request URL at
/// execution time. Interceptors can inspect this list in <c>BeforeRequest</c> to see cookies
/// that will be sent.
/// </summary>
public IReadOnlyList<Cookie> PendingCookies => _cookies;

internal void ClearPendingCookies() => _cookies.Clear();
}
9 changes: 9 additions & 0 deletions src/RestSharp/Request/RestRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ RestRequest RemoveParameter(string? name, ParameterType type) {
return p != null ? request.RemoveParameter(p) : request;
}

/// <summary>
/// Adds a cookie to the request. The cookie domain will be inferred from the request URL at execution time.
/// </summary>
/// <param name="name">Cookie name</param>
/// <param name="value">Cookie value</param>
/// <returns></returns>
public RestRequest AddCookie(string name, string value)
=> request.AddCookie(new Cookie(name, value));

/// <summary>
/// Adds cookie to the <seealso cref="HttpClient"/> cookie container.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
// Make sure we have a cookie container if not provided in the request
var cookieContainer = request.CookieContainer ??= new();

foreach (var cookie in request.PendingCookies) {
try {
cookieContainer.Add(url, cookie);
}
catch (CookieException) {
// Do not fail request if we cannot parse a cookie
}
}

request.ClearPendingCookies();

var headers = new RequestHeaders()
.AddHeaders(request.Parameters)
.AddHeaders(DefaultParameters)
Expand Down
Loading