HTTP: allow sharing http responses as variables
Is your feature request related to a problem? Please describe.
The http kernel introduction is a fantastic addition for API exploration.
In a great deal of cases, it would be useful to be able to take the response from an HTTP call and further process it in some C# (or other code).
To that extent, and similar how the sql kernel allows query results to be shared across kernels, please consider allowing http responses to be stored in variables.
Describe the solution you'd like A way to store and share http responses.
#!http --name todoOne
GET https://jsonplaceholder.typicode.com/todos/1
then in a C# cell:
#!share --from @http:todoOne todo1
todo1.Display();
As an alternative, share the entire http response and retrieve with:
#!share --from @http:todoOne todo1
todo1.Headers.Display();
todo1.Body.Display();
Describe alternatives you've considered
The #!value --from-url is also useful, but severely limited in functionality. For example, it cannot accept headers like Authorization: or execute other requests beside GET.
The only fall-back is to make the request from a code block.
This is supported today using the # @name myVariableName syntax. This and other syntax rules are the same as those described under the .http files documentation.
Thank you, @jonsequitur. It doesn't seem to quite work.
The type of version coming from the http kernel is Microsoft.DotNet.Interactive.Http.HttpNamedRequest.
However, when it gets serialized, into a System.Text.Json.JsonDocument, all its attributes except for the Name property are lost.
So one cannot seem to access the response from the HTTP call that way (with say await version.Response.ReadAsStringAsync())
Example notebook:
# @name version
GET https://trydotnet.microsoft.com/sensors/version
Accept: application/json
#!csharp
#!set --name version --value @http:version
#!csharp
version.Display();
// displays {"Name": "version"}
Example execution:
Variables and types in the inspector:
Just to add, I use this as a workaround:
### Get the public key from Azure Key Vault
# @name publicKeyRequest
GET {{keyVaultKeyUrl}}?api-version=7.4
Authorization: Bearer {{keyVaultBearer}}
###
# @publicKeyResponse {{ publicKeyRequest.response.body.$.key }}
@publicKeyResponse {{ publicKeyRequest.response.body.$.key }}
The first time you run this, it will give an error. The error is gone when you add # before the last line (making both lines identical). Run the cell, then remove the # you added, then run it again.
You can then use #!set --value @http:publicKeyResponse --name jsonWebKey to use the value in other cells.
The #!value --from-url is also useful, but severely limited in functionality. For example, it cannot accept headers like Authorization: or execute other requests beside GET.
This feature predates the HTTP kernel and this limitation suggests that #!value --from-url can probably be deprecated.