magic.lambda.http 17.2.0

dotnet add package magic.lambda.http --version 17.2.0
NuGet\Install-Package magic.lambda.http -Version 17.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="magic.lambda.http" Version="17.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add magic.lambda.http --version 17.2.0
#r "nuget: magic.lambda.http, 17.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install magic.lambda.http as a Cake Addin
#addin nuget:?package=magic.lambda.http&version=17.2.0

// Install magic.lambda.http as a Cake Tool
#tool nuget:?package=magic.lambda.http&version=17.2.0

magic.lambda.http - Invoking HTTP endpoints from Hyperlambda

The magic.lambda.http project provides HTTP invocation capabilities for Magic and Hyperlambda. More specifically the project contains the following 5 slots.

  • [http.get] - Returns some resource using the HTTP GET verb towards the specified URL
  • [http.delete] - Deletes some resource using the HTTP DELETE verb
  • [http.post] - Posts some resources to some URL using the HTTP POST verb
  • [http.put] - Puts some resources to some URL using the HTTP PUT verb
  • [http.patch] - Patches some resources to some URL using the HTTP PATCH verb

The [http.put], [http.post] and [http.patch] slots requires you to provide a [payload] or [filename] argument that will be transferred to the endpoint as is. All 5 endpoints can (optionally) take a [token] arguments, which will be transferred as a Bearer Authorization token to the endpoint in the Authorization header of your request. If you provide a [filename] argument, this is assumed to be a file relatively found within your "/files/" folder somewhere. Below is an example of retrieving the document found at the specified URL by creating an HTTP GET request.

http.get:"https://google.com"

The above will result in something resembling the following.

http.get:int:200
   headers
      Cache-Control:no-store, must-revalidate, no-cache, max-age=0
      Pragma:no-cache
      Date:"Mon, 29 Nov 2021 06:11:01 GMT"
      // ... etc ...
   content:"... content here ..."

The status code of the request is returned as the value of [http.xxx], headers returned by the server can be found in [headers] as a key/value pair, and [content] contains the actual content response object returned by the server.

All of these slots can optionally take a [timeout] argument, that overrides the default timeout of 300 seconds, with whatever integer value you provide. The timout is specified as an integer value, being the number of seconds to allow for the invocation, before the request is aborted and given up on. If they timeout period elapses, an exception will be thrown.

HTTP headers

If you want to have more control over your HTTP request, you can also explicitly add your own [headers] collection, which will become the HTTP request's headers, where the header name is the name of the node, and its value is the value of the node. Below is an example.

http.get:"https://google.com"
   headers
      Accept:text/html

If you don't add an explicit [headers] collection the invocation will assume your request payload and accepted response is JSON. If you want to change this you'll have to add at least one header to your request, at which point the default headers will not be applied.

POSTing, PUTting, and PATCHing data

The POST, PUT and PATCH slots, requires a [payload] argument, or a [filename] argument, that becomes the body of the request. Below is an example illustrating how to create a POST request, with a Bearer token to access the end resource.

http.post:"https://some-url.com"
   token:qwerty_secret_JWT_token_goes_here
   payload:some mumbo jumbo payload, typically JSON and not text though ...

Notice - If you want to submit a large file to some endpoint, without loading the file into memory first, you should rather use [filename] instead of [payload]. This ensures the file is submitted to your endpoint without loading it into memory first.

http.post:"https://some-url.com"
   filename:/README.md

Automatic conversion

You can also automatically convert the resulting response object to a lambda object if you have a registered conversion function, and you provide a [convert] argument, and set its value to boolean true. Below is an example.

http.get:"https://jsonplaceholder.typicode.com/posts"
   convert:bool:true

The above will result in something resembling the following.

http.get:int:200
   headers
      Date:"Mon, 29 Nov 2021 06:19:29 GMT"
      Transfer-Encoding:chunked
      Connection:keep-alive
      // ... etc ...
   content
      .
         userId:long:1
         id:long:1
         title:sunt aut facere repellat provident occaecati excepturi optio reprehenderit
         body:qwerty1
      .
         userId:long:1
         id:long:2
         title:qui est esse
         body:qwerty2
      // ... etc ...

The project contains automatic conversions for the following types out of the box, but you can easily register your own C# based conversion types for specific "Content-Type" values.

  • application/json
  • application/x-json
  • application/hyperlambda
  • application/x-hyperlambda
  • application/www-form-urlencoded
  • application/x-www-form-urlencoded
  • multipart/form-data

You can also convert a semantic lambda object to the correct request content in a similar fashion, by instead of providing a value to your [payload] node provide a lambda object such as illustrated below.

.userId:int:1
http.post:"https://jsonplaceholder.typicode.com/posts"
   payload
      id:int:1
      userId:x:@.userId

The above will transform your payload to a JSON object automatically for you, and also unwrap any expressions found in your lambda object before JSON transformation is applied. Automatic transformation will only be applied if you've got a transformation function registered. If you want to extend the list of supported content types to automatically transform back and forth to, you can use either Magic.Http.AddRequestHandler or MagicHttp.AddResponseHandler to add support for your own automatic transformation for both the request payload and/or the response content.

Notice - The [payload] node above must have a null value, otherwise the slot will prioritise the value, and not attempt to transform from a semantic lambda object in any ways. Values in your [payload] will be transferred as is, and you can provide byte[] arrays, streams or strings as the value of your [payload] node.

Notice - Both the URL encoded request transformer and the JSON request transformer will automatically evaluate expressions in your semantic [payload] object, but this is not true for the Hyperlambda request transformer, since in Hyperlambda it might make sense to actually pass in expressions to the endpoint. Below is an example of how to semantically pass in a Hyperlambda object to some URL.

http.post:"https://foo.com/hyperlambda-endpoint"
   headers
      Content-Type:application/hyperlambda
   payload
      .foo
         .:Thomas
         .:John
      for-each:x:@.foo
         // ... etc ...

The above will automatically serialize your lambda object as Hyperlambda, since the Content-Type is of a type supported by the automatic conversion functions, and transfer the request as a string to the endpoint, preserving expressions as is without unwrapping them before transmitting your [payload].

Passing in multipart/form-data payloads with your HTTP requests

You can also transfer multipart/form-data with all HTTP slots in Hyperlambda. This internally will use the MIME parser to semantically create a multipart message. Below is example code of how to achieve this.

http.post:"http://localhost:5000/magic/modules/foo/foo"
   headers
      Content-Type:multipart/form-data
   payload
      entity:text/plain
         headers
            Content-Disposition:"form-data; name=\"foo\""
         content:Foo bar
      entity:text/plain
         headers
            Content-Disposition:"form-data; filename=\"README.md\""
         filename:/README.md

If you create an HTTP endpoint in Hyperlambda with the filename of "/modules/foo/foo.post.hl", resembling the following, you can see how the content is transferred in your log.

.accept:multipart/form-data
request.headers.list
lambda2hyper:x:../*
log.info:x:-

How to use [http.get]

This slot accepts a URL as its value, an optionally http headers as a [headers] argument. In its simplest version it would resemble something such as the following.

http.get:"https://docs.aista.com"

The slot returns the result of the specified HTTP GET invocation, including the HTTP headers returned from the endpoint, and status code, in addition to the response object itself.

How to use [http.put]

This slot accepts a URL as its value, an optionally http headers as a [headers] argument, and a mandatory [payload] argument, being whatever payload you want to transfer to the endpoint. In its simplest version it would resemble something such as the following.

http.put:"https://some_website.com/your-put-endpoint"
   payload:@"{""foo"": ""bar""}"

The slot returns the result of the specified HTTP PUT invocation, including the HTTP headers returned from the endpoint, and status code, in addition to the response object itself. To automatically convert the response object to whatever type of response your URL returns, provide a [convert] argument, and set its value to true, such as follows.

http.put:"https://some_website.com/your-put-endpoint"
   payload:@"{""foo"": ""bar""}"
   convert:true

How to use [http.post]

This slot accepts a URL as its value, an optionally http headers as a [headers] argument, and a mandatory [payload] argument, being whatever payload you want to transfer to the endpoint. In its simplest version it would resemble something such as the following.

http.post:"https://some_website.com/your-post-endpoint"
   payload:@"{""foo"": ""bar""}"

The slot returns the result of the specified HTTP POST invocation, including the HTTP headers returned from the endpoint, and status code, in addition to the response object itself. To automatically convert the response object to whatever type of response your URL returns, provide a [convert] argument, and set its value to true, such as follows.

http.post:"https://some_website.com/your-post-endpoint"
   payload:@"{""foo"": ""bar""}"
   convert:true

How to use [http.patch]

This slot accepts a URL as its value, an optionally http headers as a [headers] argument, and a mandatory [payload] argument, being whatever payload you want to transfer to the endpoint. In its simplest version it would resemble something such as the following.

http.patch:"https://some_website.com/your-patch-endpoint"
   payload:@"{""foo"": ""bar""}"

The slot returns the result of the specified HTTP PATCH invocation, including the HTTP headers returned from the endpoint, and status code, in addition to the response object itself. To automatically convert the response object to whatever type of response your URL returns, provide a [convert] argument, and set its value to true, such as follows.

http.patch:"https://some_website.com/your-patch-endpoint"
   payload:@"{""foo"": ""bar""}"
   convert:true

How to use [http.delete]

This slot accepts a URL as its value, an optionally http headers as a [headers] argument. In its simplest version it would resemble something such as the following.

http.delete:"https://your_website.com/your-delete-endpoint"

The slot returns the result of the specified HTTP DELETE invocation, including the HTTP headers returned from the endpoint, and status code, in addition to the response object itself.

SSE or Server-Side Events

All slots also supports SSE by adding an [.sse] lambda object, which will be invoked once for each event published by the server. Your lambda object will be invoked with a [message] argument containing the raw message published by the server. You are responsible to convert the message to the correct type, since it's passed in as a UTF8 encoded raw string.

Magic's GitHub project page

Magic is 100% Open Source and you can find the primary project GitHub page here.

Project website for magic.lambda.http

The source code for this repository can be found at github.com/polterguy/magic.lambda.http, and you can provide feedback, provide bug reports, etc at the same place.

  • Build status
  • Quality Gate Status
  • Bugs
  • Code Smells
  • Coverage
  • Duplicated Lines (%)
  • Lines of Code
  • Maintainability Rating
  • Reliability Rating
  • Security Rating
  • Technical Debt
  • Vulnerabilities

The projects is copyright Thomas Hansen 2023 - 2024, and professionally maintained by AINIRO.IO.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on magic.lambda.http:

Package Downloads
magic.library

Helper project for Magic to wire up everything easily by simply adding one package, and invoking two simple methods. When using Magic, this is (probably) the only package you should actually add, since this package pulls in everything else you'll need automatically, and wires up everything sanely by default. To use package go to https://polterguy.github.io

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
17.2.0 269 1/22/2024
17.1.7 151 1/12/2024
17.1.6 123 1/11/2024
17.1.5 143 1/5/2024
17.0.1 165 1/1/2024
17.0.0 326 12/14/2023
16.11.5 305 11/12/2023
16.9.0 298 10/9/2023
16.8.4 216 9/25/2023
16.8.0 166 9/24/2023
16.7.51 138 9/24/2023
16.7.50 155 9/23/2023
16.7.0 358 7/11/2023
16.6.13 215 7/6/2023
16.4.1 317 7/2/2023
16.4.0 356 6/22/2023
16.3.1 311 6/7/2023
16.3.0 312 5/28/2023
16.1.9 588 4/30/2023
15.10.11 444 4/13/2023
15.9.1 574 3/27/2023
15.9.0 443 3/24/2023
15.8.2 483 3/20/2023
15.7.0 355 3/6/2023
15.5.0 1,529 1/28/2023
15.2.0 646 1/18/2023
15.1.0 1,118 12/28/2022
14.5.7 691 12/13/2022
14.5.5 770 12/6/2022
14.5.1 633 11/23/2022
14.5.0 574 11/18/2022
14.4.5 674 10/22/2022
14.4.1 721 10/22/2022
14.4.0 620 10/17/2022
14.3.1 1,233 9/12/2022
14.3.0 620 9/10/2022
14.1.3 891 8/7/2022
14.1.2 636 8/7/2022
14.1.1 635 8/7/2022
14.0.14 661 7/26/2022
14.0.12 666 7/24/2022
14.0.11 612 7/23/2022
14.0.10 640 7/23/2022
14.0.9 597 7/23/2022
14.0.8 704 7/17/2022
14.0.5 778 7/11/2022
14.0.4 746 7/6/2022
14.0.3 695 7/2/2022
14.0.2 652 7/2/2022
14.0.0 832 6/25/2022
13.4.0 2,006 5/31/2022
13.3.4 1,408 5/9/2022
13.3.0 933 5/1/2022
13.2.0 1,159 4/21/2022
13.1.0 991 4/7/2022
13.0.0 708 4/5/2022
11.0.5 1,403 3/2/2022
11.0.4 765 2/22/2022
11.0.3 714 2/9/2022
11.0.2 760 2/6/2022
11.0.1 742 2/5/2022
10.0.21 735 1/28/2022
10.0.20 747 1/27/2022
10.0.19 743 1/23/2022
10.0.18 717 1/17/2022
10.0.15 908 12/31/2021
10.0.14 515 12/28/2021
10.0.13 577 12/23/2021
10.0.7 1,201 12/22/2021
10.0.5 687 12/18/2021
10.0.2 629 12/14/2021
10.0.0 580 12/6/2021
9.9.9 1,108 11/29/2021
9.9.6 4,505 11/24/2021
9.9.5 444 11/23/2021
9.9.4 791 11/21/2021
9.9.3 553 11/9/2021
9.9.2 604 11/4/2021
9.9.0 701 10/30/2021
9.8.9 663 10/29/2021
9.8.7 609 10/27/2021
9.8.6 600 10/27/2021
9.8.5 691 10/26/2021
9.8.3 875 10/24/2021
9.8.2 387 10/24/2021
9.8.0 809 10/20/2021
9.7.9 612 10/19/2021
9.7.5 1,454 10/14/2021
9.7.0 826 10/9/2021
9.6.6 1,201 8/14/2021
9.3.6 3,871 6/21/2021
9.2.0 2,774 5/26/2021
9.1.4 1,256 4/21/2021
9.1.0 1,020 4/14/2021
9.0.0 851 4/5/2021
8.9.9 998 3/30/2021
8.9.3 1,500 3/19/2021
8.9.2 984 1/29/2021
8.9.1 1,001 1/24/2021
8.9.0 1,085 1/22/2021
8.6.9 2,900 11/8/2020
8.6.6 1,916 11/2/2020
8.6.0 3,872 10/28/2020
8.5.0 1,826 10/23/2020
8.4.0 5,471 10/13/2020
8.3.2 1,169 10/11/2020
8.3.1 1,902 10/5/2020
8.3.0 1,211 10/3/2020
8.2.2 1,974 9/26/2020
8.2.1 1,307 9/25/2020
8.2.0 1,337 9/25/2020
8.1.17 6,546 9/13/2020
8.1.16 605 9/13/2020
8.1.15 1,855 9/12/2020
8.1.11 2,425 9/11/2020
8.1.10 1,257 9/6/2020
8.1.9 1,280 9/3/2020
8.1.8 1,263 9/2/2020
8.1.7 1,169 8/28/2020
8.1.4 1,161 8/25/2020
8.1.3 1,237 8/18/2020
8.1.2 1,177 8/16/2020
8.1.1 1,220 8/15/2020
8.1.0 553 8/15/2020
8.0.1 2,593 8/7/2020
8.0.0 1,178 8/7/2020
7.0.1 1,320 6/28/2020
7.0.0 1,203 6/28/2020
5.0.0 7,238 2/25/2020
4.0.4 7,678 1/27/2020
4.0.3 1,228 1/27/2020
4.0.2 1,366 1/16/2020
4.0.1 1,334 1/11/2020
4.0.0 1,325 1/5/2020
3.1.0 6,091 11/10/2019
3.0.2 1,869 10/28/2019
3.0.1 1,968 10/26/2019
3.0.0 1,119 10/23/2019
2.0.1 8,050 10/15/2019
2.0.0 1,580 10/13/2019
1.1.9 1,304 10/11/2019
1.1.8 1,293 10/10/2019
1.1.7 555 10/9/2019
1.1.6 543 10/7/2019
1.1.5 562 10/6/2019
1.1.4 557 10/6/2019
1.1.2 573 10/5/2019
1.0.0 629 9/26/2019