Coder Social home page Coder Social logo

sqlclr-http-request's Introduction

sqlclr-http-request

Make HTTP Requests/Query Web APIs from T-SQL via SQLCLR

SQLCLR is a feature in Microsoft SQL Server that allows the creation of objects (stored procdures, functions, etc.) from compiled code written in one of the .NET languages, such as C#. This project uses the SQLCLR feature to create a versatile function that can make HTTP requests utilizing the .NET framework's HttpWebRequest Class. Now from SQL one can connect to and pull data from web APIs without bringing in additional technologies such as SSIS or projects written in other programming languages. There are definitely instances where a tool such as SSIS is a much better option, but for many use cases this function can simplify architecture and make integrating data a much more rapid proecess.

Also, more information can be found in the article initially posted with this function: http://www.sqlservercentral.com/articles/SQLCLR/177834/

If you're waiting for me or have any questions for me, bug me!

Table of Contents

Usage and Examples

Input parameters

  • requestMethod (string) - Most often "GET" or "POST", but there are several others used for various purposes.

  • url (string) - The URL attempting to connect to, such as an API endpoint

  • parameters (string)

    If a GET request, these will just get added into the query string. In that case you could just include them in the url parameter and pass NULL for parameters.

    Otherwise, these parameters will be converted to a byte array and added to the content of the HTTP request.

    Format of this parameter matches that of a URL query string where you have key=value pairs separated by "&": param1=A&param2=B

  • headers (string, in XML format) - This allows you to set headers for the HTTP request. They are passed as XML following this format:

  <Headers>
    <Header Name="MyHeader">My Header's Value</Header>
    <Header Name="…">…</Header>
    <Header Name="…">…</Header>
  </Headers>
  • options (string, in XML format) - This allows you to specify several options to fine-tune the HTTP Request. They are passed as XML following this format:
  <Options>
    <*option_name*>*option value*</*option_name*>
  </Options>

Available options:

  • security_protocol

    Pass a CSV of protocols from the SecurityProtocolType Enum

    Example: <security_protocol>Tls12,Tls11,Tls</security_protocol>

  • timeout

    Sets the HttpWebRequest.Timeout Property as the number of milliseconds until the request times out

    Example: <timeout>60000</timeout>

  • auto_decompress

    Sets the HttpWebRequest.AutomaticDecompression Property to automatically decompress the response

    Example: <auto_decompress>true</auto_decompress>

  • convert_response_to_base64

    Base64 encodes response. This is particularly useful if the response is a file rather than just text.

    Example: <convert_response_to_base64>true</convert_response_to_base64>

    Note, in SQL Server you're able to then decode using something like 'CAST(@string AS XML).value('.', 'VARBINARY(MAX)')'

  • debug

    Includes an element in the Response XML with info for each step of the execution

    Example: <debug>true</debug>

Returned XML

The result from this function is an XML document generated from the properties available in the HttpWebResponse Class. This is the structure of that XML.

Examples

Query stackoverflow API

SELECT 
    B.*
FROM OPENJSON
    (
        [dbo].[clr_http_request]
            (
                'GET', 'http://api.stackexchange.com/2.2/questions?site=stackoverflow', 
                NULL /* parameters */, NULL /* headers */, NULL /* options */
            ).value('Response[1]/Body[1]', 'NVARCHAR(MAX)')
    ) WITH ([items] NVARCHAR(MAX) AS JSON) A
CROSS APPLY OPENJSON(A.[items]) WITH 
    (
        [question_id] INT,
        [title] NVARCHAR(MAX),
        [tags] NVARCHAR(MAX) AS JSON,
        [is_answered] BIT,
        [view_count] INT,
        [answer_count] INT,
        [score] INT
    ) B;

This section will be updated with more examples eventually. For now, please also refer to the original article for this function: http://www.sqlservercentral.com/articles/SQLCLR/177834/

Deployment

Ensure the CLR integration is enabled on the SQL Server instance

USE [master]
GO
EXECUTE [dbo].[sp_configure] 'clr enabled', 1;
GO
RECONFIGURE;
GO

Copy ClrHttpRequest.dll to C:\ (or any preferred location, but update the following steps to reference it)

Note: The rest of these steps are all included in Deployment.sql

In the [master] database...

Create an asymmetric key from the dll

CREATE ASYMMETRIC KEY [key_clr_http_request] FROM EXECUTABLE FILE = 'C:\ClrHttpRequest.dll';

Create a login from the assymetic key and grant it UNSAFE assembly

CREATE LOGIN [lgn_clr_http_request] FROM ASYMMETRIC KEY [key_clr_http_request];
GRANT UNSAFE ASSEMBLY TO [lgn_clr_http_request];

In the desired user database...

Create a user for the login just created

CREATE USER [usr_clr_http_request] FOR LOGIN [lgn_clr_http_request];

Create the assembly from the dll

CREATE ASSEMBLY [ClrHttpRequest] FROM 'C:\ClrHttpRequest.dll' WITH PERMISSION_SET=EXTERNAL_ACCESS;

Create the clr_http_request function

CREATE FUNCTION [dbo].[clr_http_request] (@requestMethod NVARCHAR(MAX), @url NVARCHAR(MAX), @parameters NVARCHAR(MAX), @headers NVARCHAR(MAX), @optionsXml NVARCHAR(MAX))
RETURNS XML AS EXTERNAL NAME [ClrHttpRequest].[UserDefinedFunctions].[clr_http_request];

A quick test to confirm it works

SELECT [dbo].[clr_http_request]('GET', 'https://github.com/eilerth/sqlclr-http-request/', NULL, NULL, '<Options><security_protocol>Tls12</security_protocol></Options>');

Should this be a native function in SQL Server?

If you think so, you should vote for it here: https://feedback.azure.com/forums/908035-sql-server/suggestions/34429699-http-request-function

License

This project is licensed under the MIT License - see the LICENSE.md file for details

sqlclr-http-request's People

Contributors

eilerth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sqlclr-http-request's Issues

CREATE ASSEMBLY for assembly 'ClrHttpRequest' failed because assembly 'ClrHttpRequest' is not trusted.

I was following your deploy and I incurred in this error:

Msg 10327, Level 14, State 1, Line 38
CREATE ASSEMBLY for assembly 'ClrHttpRequest' failed because assembly 'ClrHttpRequest' is not trusted. The assembly is trusted when either of the following is true: the assembly is signed with a certificate or an asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission, or the assembly is trusted using sp_add_trusted_assembly.
Msg 6528, Level 16, State 1, Procedure clr_http_request, Line 2 [Batch Start Line 39]
Assembly 'ClrHttpRequest' was not found in the SQL catalog of database 'db_clr_http_request'.

How to avoid it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.