GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||
GeezerVison is showing fsprojects.github.io — HTTP Utilities HTTP UtilitiesThe .NET library provides a powerful API for creating and sending HTTP web requests.
There is a simple The FSharp.Data package provides a simple Http type with four methods: Http.RequestString and Http.AsyncRequestString, that can be used to create a simple request and perform it synchronously or asynchronously, and Http.Request and it's async companion Http.AsyncRequest if you want to request binary files or you want to know more about the response like the status code, the response URL, or the returned headers and cookies. The type is located in
Sending simple requestsTo send a simple HTTP (GET) request that downloads a specified web page, you can use Http.RequestString and Http.AsyncRequestString with just a single parameter:
In the rest of the documentation, we focus on the Query parameters and headersYou can specify query parameters either by constructing
an URL that includes the parameters (e.g.
Additional headers are specified similarly - using an optional parameter The following example uses The Movie Database API to search for the word "batman". To run the sample, you'll need to register and provide your API key:
The library supports a simple and unchecked string based API (used in the previous example),
but you can also use pre-defined header names to avoid spelling mistakes. The named headers
are available in HttpRequestHeaders (and HttpResponseHeaders) modules, so you can either
use the full name
Getting extra informationNote that in the previous snippet, if you don't specify a valid API key, you'll get a (401) Unauthorized error,
and that will throw an exception. Unlike when using You can also opt out of the exception by specifying the
In this case, you might want to look at the HTTP status code so you don't confuse an error message for an actual response. If you want to see more information about the response, including the status code, the response headers, the returned cookies, and the response url (which might be different to the url you passed when there are redirects), you can use the Http.Request method instead of the Http.RequestString method:
Sending request dataIf you want to create a POST request with HTTP POST data, you can specify the
additional data in the
If you specify a body, you do not need to set the The following example uses the httpbin.org service which returns the request details:
By default, the
Maintaining cookies across requestsIf you want to maintain cookies between requests, you can specify the The following is an old sample showing how this is set.
Requesting binary dataThe Http.RequestString method will always return the response as a
HTTP AuthenticationFSharp.Data provides built-in helpers in HttpRequestHeaders for the most common authentication schemes. Basic authenticationUse HttpRequestHeaders.BasicAuth to add an
Bearer / token authenticationUse HttpRequestHeaders.Authorization to send any other
Windows / NTLM integrated authenticationFor services that require Windows Integrated Authentication (NTLM or Negotiate), use the
Customizing the HTTP requestFor the cases where you need something not natively provided by the library, you can use the
As an example, let's say you want to add a client certificate to your request. To do that,
you need to open the Assuming the certificate is stored in
Handling multipart form dataYou can also send http multipart form data via the
Paginated APIs (RFC 5988 Link headers)Many REST APIs — including GitHub, GitLab, and others — use the
The Http.ParseLinkHeader utility parses such a header into a
Related articles
Multiple items
namespace FSharp -------------------- namespace Microsoft.FSharp Multiple items
namespace FSharp.Data -------------------- namespace Microsoft.FSharp.Data type Http =
static member AppendQueryToUrl: url: string * query: (string * string) list -> string
static member AsyncRequest: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<HttpResponse>
static member AsyncRequestStream: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<HttpResponseWithStream>
static member AsyncRequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<string>
static member ParseLinkHeader: linkHeader: string -> Map<string,string>
static member Request: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> HttpResponse
static member RequestStream: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> HttpResponseWithStream
static member RequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> string
<summary> Utilities for working with network via HTTP. Includes methods for downloading resources with specified headers, query parameters and HTTP body </summary> static member Http.RequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: System.Net.CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (System.Net.HttpWebRequest -> System.Net.HttpWebRequest) * ?timeout: int -> string
val async: AsyncBuilder
val html: string
static member Http.AsyncRequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: System.Net.CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (System.Net.HttpWebRequest -> System.Net.HttpWebRequest) * ?timeout: int -> Async<string>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property System.String.Length: int with get
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ... -------------------- type Async<'T> static member Async.Start: computation: Async<unit> * ?cancellationToken: System.Threading.CancellationToken -> unit
val query: Linq.QueryBuilder
val apiKey: string
module HttpRequestHeaders
from FSharp.Data
<summary> Headers that can be sent in an HTTP request </summary> val Accept: contentType: string -> string * string
<summary> Content-Types that are acceptable for the response </summary> module HttpContentTypes
from FSharp.Data
<summary> Constants for common HTTP content types </summary>
val Json: string = "application/json"
<summary> application/json </summary> val response: HttpResponse
static member Http.Request: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: System.Net.CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (System.Net.HttpWebRequest -> System.Net.HttpWebRequest) * ?timeout: int -> HttpResponse
HttpResponse.Headers: Map<string,string>
<summary> If the same header is present multiple times, the values will be concatenated with comma as the separator </summary> HttpResponse.Cookies: Map<string,string>
HttpResponse.ResponseUrl: string
HttpResponse.StatusCode: int
union case HttpRequestBody.FormValues: (string * string) seq -> HttpRequestBody
val ContentType: contentType: string -> string * string
<summary> The MIME type of the body of the request (used with POST and PUT requests) </summary> union case HttpRequestBody.TextRequest: string -> HttpRequestBody
val msdnUrl: className: string -> string
val className: string
val root: string
val sprintf: format: Printf.StringFormat<'T> -> 'T
val docInCSharp: string
System.String.Contains(value: string) : bool
System.String.Contains(value: char) : bool System.String.Contains(value: string, comparisonType: System.StringComparison) : bool System.String.Contains(value: char, comparisonType: System.StringComparison) : bool namespace System
namespace System.Net
val cc: CookieContainer
Multiple items
type CookieContainer = new: unit -> unit + 2 overloads member Add: cookie: Cookie -> unit + 3 overloads member GetAllCookies: unit -> CookieCollection member GetCookieHeader: uri: Uri -> string member GetCookies: uri: Uri -> CookieCollection member SetCookies: uri: Uri * cookieHeader: string -> unit static val DefaultCookieLengthLimit: int static val DefaultCookieLimit: int static val DefaultPerDomainCookieLimit: int member Capacity: int ... <summary>Provides a container for a collection of <see cref="T:System.Net.CookieCollection" /> objects.</summary> -------------------- CookieContainer() : CookieContainer CookieContainer(capacity: int) : CookieContainer CookieContainer(capacity: int, perDomainCapacity: int, maxCookieSize: int) : CookieContainer Multiple items
namespace System.Net.Http -------------------- type Http = static member AppendQueryToUrl: url: string * query: (string * string) list -> string static member AsyncRequest: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<HttpResponse> static member AsyncRequestStream: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<HttpResponseWithStream> static member AsyncRequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> Async<string> static member ParseLinkHeader: linkHeader: string -> Map<string,string> static member Request: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> HttpResponse static member RequestStream: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> HttpResponseWithStream static member RequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> string <summary> Utilities for working with network via HTTP. Includes methods for downloading resources with specified headers, query parameters and HTTP body </summary> static member Http.RequestString: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> string
val ignore: value: 'T -> unit
val docInFSharp: string
val logoUrl: string
static member Http.Request: url: string * ?query: (string * string) list * ?headers: (string * string) seq * ?httpMethod: string * ?body: HttpRequestBody * ?cookies: (string * string) seq * ?cookieContainer: CookieContainer * ?silentHttpErrors: bool * ?silentCookieErrors: bool * ?responseEncodingOverride: string * ?customizeHttpRequest: (HttpWebRequest -> HttpWebRequest) * ?timeout: int -> HttpResponse
union case HttpResponseBody.Text: string -> HttpResponseBody
val text: string
union case HttpResponseBody.Binary: byte array -> HttpResponseBody
val bytes: byte array
property System.Array.Length: int with get
<summary>Gets the total number of elements in all the dimensions of the <see cref="T:System.Array" />.</summary> <exception cref="T:System.OverflowException">The array is multidimensional and contains more than <see cref="F:System.Int32.MaxValue">Int32.MaxValue</see> elements.</exception> <returns>The total number of elements in all the dimensions of the <see cref="T:System.Array" />; zero if there are no elements in the array.</returns> val BasicAuth: username: string -> password: string -> string * string
<summary> Authentication header using Basic Auth encoding </summary> val token: string
val Authorization: credentials: string -> string * string
<summary> Authentication credentials for HTTP authentication </summary> val UserAgent: userAgent: string -> string * string
<summary> The user agent string of the user agent </summary> val req: HttpWebRequest
property HttpWebRequest.UseDefaultCredentials: bool with get, set
property HttpWebRequest.Credentials: ICredentials with get, set
Multiple items
type NetworkCredential = interface ICredentials interface ICredentialsByHost new: unit -> unit + 4 overloads member GetCredential: host: string * port: int * authenticationType: string -> NetworkCredential + 1 overload member Domain: string member Password: string member SecurePassword: SecureString member UserName: string <summary>Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.</summary> -------------------- NetworkCredential() : NetworkCredential NetworkCredential(userName: string, password: System.Security.SecureString) : NetworkCredential NetworkCredential(userName: string, password: string) : NetworkCredential NetworkCredential(userName: string, password: System.Security.SecureString, domain: string) : NetworkCredential NetworkCredential(userName: string, password: string, domain: string) : NetworkCredential namespace System.Security
namespace System.Security.Cryptography
namespace System.Security.Cryptography.X509Certificates
val clientCert: X509Certificate2
Multiple items
type X509Certificate2 = inherit X509Certificate new: unit -> unit + 15 overloads member CopyWithPrivateKey: privateKey: CompositeMLDsa -> X509Certificate2 + 4 overloads member ExportCertificatePem: unit -> string member GetCompositeMLDsaPrivateKey: unit -> CompositeMLDsa member GetCompositeMLDsaPublicKey: unit -> CompositeMLDsa member GetECDiffieHellmanPrivateKey: unit -> ECDiffieHellman member GetECDiffieHellmanPublicKey: unit -> ECDiffieHellman member GetMLDsaPrivateKey: unit -> MLDsa member GetMLDsaPublicKey: unit -> MLDsa ... <summary>Represents an X.509 certificate.</summary> -------------------- X509Certificate2(handle: nativeint) : X509Certificate2 X509Certificate2(certificate: X509Certificate) : X509Certificate2 property HttpWebRequest.ClientCertificates: X509CertificateCollection with get, set
X509CertificateCollection.Add(value: X509Certificate) : int
val largeFilePath: string
val data: System.IO.Stream
namespace System.IO
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: string seq -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: string seq * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: ReadOnlySpan<char> -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: ReadOnlyMemory<char> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> System.IO.File.OpenRead(path: string) : System.IO.FileStream
type Stream =
inherit MarshalByRefObject
interface IAsyncDisposable
interface IDisposable
member BeginRead: buffer: byte array * offset: int * count: int * callback: AsyncCallback * state: obj -> IAsyncResult
member BeginWrite: buffer: byte array * offset: int * count: int * callback: AsyncCallback * state: obj -> IAsyncResult
member Close: unit -> unit
member CopyTo: destination: Stream -> unit + 1 overload
member CopyToAsync: destination: Stream -> Task + 3 overloads
member Dispose: unit -> unit
member DisposeAsync: unit -> ValueTask
...
<summary>Provides a generic view of a sequence of bytes. This is an abstract class.</summary> union case HttpRequestBody.Multipart: boundary: string * parts: MultipartItem seq -> HttpRequestBody
<summary> A sequence of formParamName * fileName * fileContent groups </summary> Multiple items
union case MultipartItem.MultipartItem: formField: string * filename: string * content: System.IO.Stream -> MultipartItem -------------------- type MultipartItem = | MultipartItem of formField: string * filename: string * content: Stream type Path =
static member ChangeExtension: path: string * extension: string -> string
static member Combine: path1: string * path2: string -> string + 4 overloads
static member EndsInDirectorySeparator: path: ReadOnlySpan<char> -> bool + 1 overload
static member Exists: path: string -> bool
static member GetDirectoryName: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload
static member GetExtension: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload
static member GetFileName: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload
static member GetFileNameWithoutExtension: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload
static member GetFullPath: path: string -> string + 1 overload
static member GetInvalidFileNameChars: unit -> char array
...
<summary>Performs operations on <see cref="T:System.String" /> instances that contain file or directory path information. These operations are performed in a cross-platform manner.</summary>
System.IO.Path.GetFileName(path: string) : string
System.IO.Path.GetFileName(path: System.ReadOnlySpan<char>) : System.ReadOnlySpan<char> type Release = obj
val fetchAllReleases: unit -> 'a array
val loop: url: string -> acc: 'b array -> 'b array
val url: string
val acc: 'b array
val items: 'b array
HttpResponse.Body: HttpResponseBody
val acc': 'b array
module Array
from Microsoft.FSharp.Collections
val append: array1: 'T array -> array2: 'T array -> 'T array
Multiple items
module Map from Microsoft.FSharp.Collections -------------------- type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ... -------------------- new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> val tryFind: key: 'Key -> table: Map<'Key,'T> -> 'T option (requires comparison)
module HttpResponseHeaders
from FSharp.Data
<summary> Headers that can be received in an HTTP response </summary>
val Link: string = "Link"
<summary> Used to express a typed relationship with another resource, where the relation type is defined by RFC 5988 </summary> union case Option.Some: Value: 'T -> Option<'T>
val linkHeader: string
static member Http.ParseLinkHeader: linkHeader: string -> Map<string,string>
val nextUrl: string
union case Option.None: Option<'T>
| |||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |