|
1 | 1 | # sqlclr-http-request
|
2 | 2 |
|
3 | 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. |
| 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](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest). 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 | 5 |
|
6 | 6 | I'm going to initially link to the article initially posted with this and complete more documentation later:
|
7 | 7 | http://www.sqlservercentral.com/articles/SQLCLR/177834/
|
@@ -35,60 +35,73 @@ If you're waiting for me or have any questions for me, bug me!
|
35 | 35 | ```
|
36 | 36 |
|
37 | 37 | - 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:
|
38 |
| - |
| 38 | +``` |
39 | 39 | <Options>
|
40 | 40 | <*option_name*>*option value*</*option_name*>
|
41 | 41 | </Options>
|
42 |
| - |
43 |
| - Available options: |
44 |
| - - security_protocol |
| 42 | +``` |
| 43 | + |
| 44 | +#### Available options: |
| 45 | +- security_protocol |
45 | 46 |
|
46 |
| - Pass a CSV of protocols from the [SecurityProtocolType Enum](https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype) |
| 47 | + Pass a CSV of protocols from the [SecurityProtocolType Enum](https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype) |
47 | 48 |
|
48 |
| - Example: '<security_protocol>Tls12,Tls11,Tls</security_protocol>' |
| 49 | + Example: `<security_protocol>Tls12,Tls11,Tls</security_protocol>` |
49 | 50 |
|
50 |
| - - timeout |
51 |
| - Sets the [HttpWebRequest.Timeout Property](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.timeout) as the number of milliseconds until the request times out |
52 |
| - Example: '<timeout>60000</timeout>' is 60,000 milliseconds, which is 60 seconds (1 minute). |
53 |
| - - auto_decompress |
54 |
| - Sets the [HttpWebRequest.AutomaticDecompression Property](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.automaticdecompression) to automatically decompress the response |
55 |
| - Example: '<auto_decompress>true</auto_decompress>' |
56 |
| - - convert_response_to_base64 |
57 |
| - Base64 encodes response. This is particularly useful if the response is a file rather than just text. |
58 |
| - Example: '<convert_response_to_base64>true</convert_response_to_base64> |
59 |
| - Note, in SQL Server you're able to then decode using something like 'CAST(@string AS XML).value(\'.\', \'VARBINARY(MAX)\')' |
60 |
| - - debug |
61 |
| - Includes an element in the Response XML with info for each step of the execution |
62 |
| - Example: '<debug>true</debug> |
| 51 | +- timeout |
| 52 | + |
| 53 | + Sets the [HttpWebRequest.Timeout Property](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.timeout) as the number of milliseconds until the request times out |
| 54 | + |
| 55 | + Example: `<timeout>60000</timeout>` |
| 56 | + |
| 57 | +- auto_decompress |
| 58 | + |
| 59 | + Sets the [HttpWebRequest.AutomaticDecompression Property](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.automaticdecompression) to automatically decompress the response |
| 60 | + |
| 61 | + Example: `<auto_decompress>true</auto_decompress>` |
| 62 | + |
| 63 | +- convert_response_to_base64 |
| 64 | + |
| 65 | + Base64 encodes response. This is particularly useful if the response is a file rather than just text. |
| 66 | + |
| 67 | + Example: `<convert_response_to_base64>true</convert_response_to_base64>` |
| 68 | + |
| 69 | + Note, in SQL Server you're able to then decode using something like 'CAST(@string AS XML).value(\'.\', \'VARBINARY(MAX)\')' |
| 70 | + |
| 71 | +- debug |
| 72 | + |
| 73 | + Includes an element in the Response XML with info for each step of the execution |
| 74 | + |
| 75 | + Example: `<debug>true</debug>` |
63 | 76 |
|
64 | 77 | ### Returned XML
|
65 | 78 |
|
66 | 79 | The result from this function is an XML document generated from the properties available in the [HttpWebResponse Class](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse). This is the structure of that XML.
|
67 | 80 |
|
68 | 81 | - Response - this is the root element
|
69 |
| - - [CharacterSet](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.CharacterSet) |
70 |
| - - [ContentEncoding](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentEncoding) |
71 |
| - - [ContentLength](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentLength) |
72 |
| - - [ContentType](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentType) |
73 |
| - - HeadersCount - Count of [Headers](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Headers) |
74 |
| - - [IsFromCache](https://docs.microsoft.com/en-us/dotnet/api/system.net.webresponse.isfromcache) |
75 |
| - - [IsMutuallyAuthenticated](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.IsMutuallyAuthenticated) |
76 |
| - - [LastModified](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.LastModified) |
77 |
| - - [Method](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Method) |
78 |
| - - [ProtocolVersion](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ProtocolVersion) |
79 |
| - - [ResponseUri](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ResponseUri) |
80 |
| - - [StatusCode](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusCode) |
81 |
| - - [Server](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Server) |
82 |
| - - StatusNumber - Number derived from [StatusCode](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusCode) |
83 |
| - - [StatusDescription](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusDescription) |
84 |
| - - [SupportsHeaders](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.SupportsHeaders) |
85 |
| - - [Headers](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Headers) |
86 |
| - * Header - each header will get its own node here |
87 |
| - - Name |
88 |
| - - Values - a header can have multiple values in C#'s HttpWebResponse |
89 |
| - - Value |
90 |
| - - Body - Content from the response |
91 |
| - - Debug - Log and info for each step |
| 82 | + - [CharacterSet](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.CharacterSet) |
| 83 | + - [ContentEncoding](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentEncoding) |
| 84 | + - [ContentLength](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentLength) |
| 85 | + - [ContentType](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ContentType) |
| 86 | + - HeadersCount - Count of [Headers](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Headers) |
| 87 | + - [IsFromCache](https://docs.microsoft.com/en-us/dotnet/api/system.net.webresponse.isfromcache) |
| 88 | + - [IsMutuallyAuthenticated](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.IsMutuallyAuthenticated) |
| 89 | + - [LastModified](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.LastModified) |
| 90 | + - [Method](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Method) |
| 91 | + - [ProtocolVersion](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ProtocolVersion) |
| 92 | + - [ResponseUri](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.ResponseUri) |
| 93 | + - [StatusCode](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusCode) |
| 94 | + - [Server](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Server) |
| 95 | + - StatusNumber - Number derived from [StatusCode](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusCode) |
| 96 | + - [StatusDescription](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.StatusDescription) |
| 97 | + - [SupportsHeaders](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.SupportsHeaders) |
| 98 | + - [Headers](https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.Headers) |
| 99 | + * Header - each header will get its own node here |
| 100 | + - Name |
| 101 | + - Values - a header can have multiple values in C#'s HttpWebResponse |
| 102 | + - Value |
| 103 | + - Body - Content from the response |
| 104 | + - Debug - Log and info for each step |
92 | 105 |
|
93 | 106 | ### Examples
|
94 | 107 |
|
|
0 commit comments