Skip to content

[API Proposal]: HttpMethod.From(string) #89161

Description

@stephentoub

Background and motivation

HttpMethod provides a constructor (string method = ...; ... = new HttpMethod(method);), but the 99.999% case is that one of the static properties on HttpMethod suffices, and folks end up writing their own mapping tables to use those singletons. We can just provide that mapping once.

API Proposal

namespace System.Net.Http;

public class HttpMethod
{
// Case insensitive - to match our logic in Networking stack
+   public static HttpMethod From(ReadOnlySpan<char> method);
// Alternative names: Parse / Get / Create

//   public static HttpMethod From(string method); ... OPTIONAL, Networking team thinks it is NOT necessary

/*
        method switch
        {
            "GET" => s_getMethod,
            "PUT" => s_putMethod,
            "POST" => s_postMethod,
            "DELETE" => s_deleteMethod,
            "HEAD" => s_headMethod,
            "OPTIONS" => s_optionsMethod,
            "TRACE" => s_traceMethod,
            "PATCH" => s_patchMethod,
            "CONNECT" => s_connectMethod,
            _ => new HttpMethod(method);
        };
*/
} 

API Usage

string methodName = ...;
HttpMethod method = HttpMethod.From(methodName);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions