|
1 | 1 | # sqlclr-http-request
|
2 |
| -Make HTTP Requests from T-SQL via SQLCLR |
| 2 | + |
| 3 | +Make HTTP Requests/Query Web APIs from T-SQL via SQLCLR |
| 4 | +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. |
| 5 | + |
| 6 | +I'm going to initially link to the article initially posted with this and complete more documentation later: |
| 7 | +http://www.sqlservercentral.com/articles/SQLCLR/177834/ |
| 8 | + |
| 9 | +If you're waiting for me or have any questions for me, bug me! |
| 10 | + |
| 11 | +## Deployment |
| 12 | + |
| 13 | +### Ensure the CLR integration is enabled on the SQL Server instance |
| 14 | +``` |
| 15 | +USE [master] |
| 16 | +GO |
| 17 | +EXECUTE [dbo].[sp_configure] 'clr enabled', 1; |
| 18 | +GO |
| 19 | +RECONFIGURE; |
| 20 | +GO |
| 21 | +``` |
| 22 | + |
| 23 | +### *Note:* The rest of these steps are all included in Deployment.sql |
| 24 | + |
| 25 | +### In the [master] database... |
| 26 | + |
| 27 | +Create an asymmetric key from the compiled dll |
| 28 | +``` |
| 29 | +CREATE ASYMMETRIC KEY [key_clr_http_request] FROM EXECUTABLE FILE = 'C:\ClrHttpRequest.dll'; |
| 30 | +``` |
| 31 | + |
| 32 | +Create a login from the assymetic key and grant it UNSAFE assembly |
| 33 | +``` |
| 34 | +CREATE LOGIN [lgn_clr_http_request] FROM ASYMMETRIC KEY [key_clr_http_request]; |
| 35 | +GRANT UNSAFE ASSEMBLY TO [lgn_clr_http_request]; |
| 36 | +``` |
| 37 | + |
| 38 | +### In the desired user database... |
| 39 | +Create a user for the login just created |
| 40 | +``` |
| 41 | +CREATE USER [usr_clr_http_request] FOR LOGIN [lgn_clr_http_request]; |
| 42 | +``` |
| 43 | + |
| 44 | +Create the assembly from the dll |
| 45 | +``` |
| 46 | +CREATE ASSEMBLY [ClrHttpRequest] FROM 'C:\ClrHttpRequest.dll' WITH PERMISSION_SET=EXTERNAL_ACCESS; |
| 47 | +``` |
| 48 | + |
| 49 | +Create the clr_http_request function |
| 50 | +``` |
| 51 | +CREATE FUNCTION [dbo].[clr_http_request] (@requestMethod NVARCHAR(MAX), @url NVARCHAR(MAX), @parameters NVARCHAR(MAX), @headers NVARCHAR(MAX), @optionsXml NVARCHAR(MAX)) |
| 52 | +RETURNS XML AS EXTERNAL NAME [ClrHttpRequest].[UserDefinedFunctions].[clr_http_request]; |
| 53 | +``` |
| 54 | + |
| 55 | +### A quick test to confirm it works |
| 56 | +``` |
| 57 | +SELECT [dbo].[clr_http_request]('GET', 'https://github.com/eilerth/sqlclr-http-request/', NULL, NULL, '<security_protocol>Tls12</security_protocol>'); |
| 58 | +``` |
| 59 | + |
| 60 | +## License |
| 61 | + |
| 62 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
0 commit comments