Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppendPathSegment trims the trailing slash #37

Closed
AbraamNader opened this issue Jun 24, 2015 · 1 comment
Closed

AppendPathSegment trims the trailing slash #37

AbraamNader opened this issue Jun 24, 2015 · 1 comment

Comments

@AbraamNader
Copy link

AppendPathSegment trims the trailing slash when calling API's like this:

// Example: "https://api.site.com/v1/endpoint/?param=value"
var URL = "https://api.site.com/v1/".AppendPathSegment("endpoint").SetQueryParam("param", value)
// Outputs: "https://api.site.com/v1/endpoint?param=value"

Notice the slash after "endpoint", without that slash "/" the API call would fail or improperly redirect (301)

And for now as you suggested, I made an extension method, so it wouldn't break the fluent syntax.
It's different than what you suggested but this gives more control, since this is a special case, most APIs don't have this slash at the end.

namespace Flurl.Extensions
{
    /// <summary>
    /// A set of string extension methods for working with Flurl URLs
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Explicitly adds a '/' character to the end of the Url.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Url AppendPathSeparator(this string url)
        {
            if (!string.IsNullOrEmpty(url) && !url.EndsWith("/")) url += "/";
            return new Url(url);
        }

        /// <summary>
        /// Explicitly adds a '/' character to the end of the Url.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Url AppendPathSeparator(this Url url)
        {
            if (url != null && !string.IsNullOrEmpty(url.Path) && !url.Path.EndsWith("/")) url += "/";
            return new Url(url);
        }
    }
}

I use it like this:

var URL = "https://api.site.com/v1/".AppendPathSegment("endpoint").AppendPathSeparator().SetQueryParam("param", value)
// Outputs: "https://api.site.com/v1/endpoint/?param=value"
@tmenier
Copy link
Owner

tmenier commented Jun 25, 2015

I'm of the opinion (and I hope this works for you) that the best solution is for Flurl to simply not trim the trailing slash when it's explicitly provided. So you can call url.AppendPathSegment("endpoint/") and it'll be left alone.

The new package with this update is up on NuGet.

@tmenier tmenier closed this as completed Jun 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants