Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/Flurl.CodeGen/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public static IEnumerable<ExtensionMethod> GetRequestReturningExtensions(MethodA
yield return Create("WithCookies", "Creates a new FlurlRequest and adds name-value pairs to its Cookie header based on property names/values of the provided object, or keys/values if object is a dictionary. " +
"To automatically maintain a cookie \"session\", consider using a CookieJar or CookieSession instead.")
.AddArg("values", "object", "Names/values of HTTP cookies to set. Typically an anonymous object or IDictionary.");
yield return Create("WithCookies", "Creates a new FlurlRequest and adds name-value pairs to its Cookie header. " +
"To automatically maintain a cookie \"session\", consider using a CookieJar or CookieSession instead.")
.AddArg("values", "IEnumerable<(string Key, string Value)>", "Names/values of HTTP cookies to set.");
yield return Create("WithCookies", "Creates a new FlurlRequest and sets the CookieJar associated with this request, which will be updated with any Set-Cookie headers present in the response and is suitable for reuse in subsequent requests.")
.AddArg("cookieJar", "CookieJar", "The CookieJar.");
yield return Create("WithCookies", "Creates a new FlurlRequest and associates it with a new CookieJar, which will be updated with any Set-Cookie headers present in the response and is suitable for reuse in subsequent requests.")
Expand Down
1 change: 1 addition & 0 deletions src/Flurl.CodeGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static int Main(string[] args) {
writer
.WriteLine("// This file was auto-generated by Flurl.CodeGen. Do not edit directly.")
.WriteLine("using System;")
.WriteLine("using System.Collections.Generic;")
.WriteLine("using System.IO;")
.WriteLine("using System.Net;")
.WriteLine("using System.Net.Http;")
Expand Down
25 changes: 21 additions & 4 deletions src/Flurl.Http/CookieExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Flurl.Util;

Expand Down Expand Up @@ -30,15 +31,31 @@ public static IFlurlRequest WithCookie(this IFlurlRequest request, string name,
/// <param name="request">The IFlurlRequest.</param>
/// <param name="values">Names/values of HTTP cookies to set. Typically an anonymous object or IDictionary.</param>
/// <returns>This IFlurlClient.</returns>
public static IFlurlRequest WithCookies(this IFlurlRequest request, object values) {
public static IFlurlRequest WithCookies(this IFlurlRequest request, object values)
{
var kvPairs = values.ToKeyValuePairs()
.Select(kv => (kv.Key, kv.Value.ToInvariantString()));
return request.WithCookies(kvPairs);
}

/// <summary>
/// Adds or updates name-value pairs in this request's Cookie header.
/// To automatically maintain a cookie "session", consider using a CookieJar or CookieSession instead.
/// </summary>
/// <param name="request">The IFlurlRequest.</param>
/// <param name="values">Names/values of HTTP cookies to set.</param>
/// <returns>This IFlurlClient.</returns>
public static IFlurlRequest WithCookies(this IFlurlRequest request, IEnumerable<(string Key, string Value)> values)
{
var cookies = new NameValueList<string>(request.Cookies, true); // cookie names are case-sensitive https://stackoverflow.com/a/11312272/62600
// although rare, we need to accommodate the possibility of multiple cookies with the same name
foreach (var group in values.ToKeyValuePairs().GroupBy(x => x.Key)) {
foreach (var group in values.GroupBy(x => x.Key))
{
// add or replace the first one (by name)
cookies.AddOrReplace(group.Key, group.First().Value.ToInvariantString());
cookies.AddOrReplace(group.Key, group.First().Value);
// append the rest
foreach (var kv in group.Skip(1))
cookies.Add(kv.Key, kv.Value.ToInvariantString());
cookies.Add(kv.Key, kv.Value);
}
return request.WithHeader("Cookie", CookieCutter.BuildRequestHeader(cookies));
}
Expand Down
31 changes: 31 additions & 0 deletions src/Flurl.Http/GeneratedExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This file was auto-generated by Flurl.CodeGen. Do not edit directly.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -632,6 +633,16 @@ public static IFlurlRequest WithCookies(this Url url, object values) {
return new FlurlRequest(url).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and adds name-value pairs to its Cookie header. To automatically maintain a cookie "session", consider using a CookieJar or CookieSession instead.
/// </summary>
/// <param name="url">This Flurl.Url.</param>
/// <param name="values">Names/values of HTTP cookies to set.</param>
/// <returns>A new IFlurlRequest.</returns>
public static IFlurlRequest WithCookies(this Url url, IEnumerable<(string Key, string Value)> values) {
return new FlurlRequest(url).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and sets the CookieJar associated with this request, which will be updated with any Set-Cookie headers present in the response and is suitable for reuse in subsequent requests.
/// </summary>
Expand Down Expand Up @@ -1151,6 +1162,16 @@ public static IFlurlRequest WithCookies(this string url, object values) {
return new FlurlRequest(url).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and adds name-value pairs to its Cookie header. To automatically maintain a cookie "session", consider using a CookieJar or CookieSession instead.
/// </summary>
/// <param name="url">This URL.</param>
/// <param name="values">Names/values of HTTP cookies to set.</param>
/// <returns>A new IFlurlRequest.</returns>
public static IFlurlRequest WithCookies(this string url, IEnumerable<(string Key, string Value)> values) {
return new FlurlRequest(url).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and sets the CookieJar associated with this request, which will be updated with any Set-Cookie headers present in the response and is suitable for reuse in subsequent requests.
/// </summary>
Expand Down Expand Up @@ -1670,6 +1691,16 @@ public static IFlurlRequest WithCookies(this Uri uri, object values) {
return new FlurlRequest(uri).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and adds name-value pairs to its Cookie header. To automatically maintain a cookie "session", consider using a CookieJar or CookieSession instead.
/// </summary>
/// <param name="uri">This System.Uri.</param>
/// <param name="values">Names/values of HTTP cookies to set.</param>
/// <returns>A new IFlurlRequest.</returns>
public static IFlurlRequest WithCookies(this Uri uri, IEnumerable<(string Key, string Value)> values) {
return new FlurlRequest(uri).WithCookies(values);
}

/// <summary>
/// Creates a new FlurlRequest and sets the CookieJar associated with this request, which will be updated with any Set-Cookie headers present in the response and is suitable for reuse in subsequent requests.
/// </summary>
Expand Down
Loading