I wanted to send an HTTP request from SQL server to Tomcat server. I have installed SQL server 2012 express and non .NET application in Tomcat server. I have gone through this like Make a HTTP request from SQL server
As it says in the above article, "The COM object WinHttp.WinHttpRequest.5.1 must be installed on the server, some typical variations are WinHttp.WinHttpRequest.5". I have downloaded winhttp.zip from the winhttp download link, found winhttp.dll in the zip folder and pasted it in C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER2\MSSQL\Binn
as suggested in this msdn link.
Following that same advice, I have executed following line in SSMS:
sp_addextendedproc 'GetHttp', 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER2\MSSQL\Binn\winhttp.dll';
I also executed the following code in SSMS as said in "Make an HTTP request from SQL server link":
Alter function GetHttp ( @url varchar(8000) ) returns varchar(8000) as BEGIN DECLARE @win int DECLARE @hr int DECLARE @text varchar(8000) EXEC @hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @win EXEC @hr=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false' IF @hr <> 0 EXEC sp_OAGetErrorInfo @win EXEC @hr=sp_OAMethod @win,'Send' IF @hr <> 0 EXEC sp_OAGetErrorInfo @win EXEC @hr=sp_OAGetProperty @win,'ResponseText',@text OUTPUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @win EXEC @hr=sp_OADestroy @win IF @hr <> 0 EXEC sp_OAGetErrorInfo @win RETURN @text END
Then I get the error
Msg 2010, Level 16, State 1, Procedure GetHttp, Line 2
Cannot perform alter on 'GetHttp' because it is an incompatible object type.
I do not know how to call the function to send the HTTP request. I assume it is something like this GetHttp('http://www.google.co.in/')
.
What am I missing?
A basic requirement is you have to have a windows instance with MS SQL server because SQL server uses a predefined stored procedure to fire HTTP request which is supported only on windows! (It's Microsoft, you know!) You can use SQL Server Management Studio (SSMS) editor as an interface which is quite user-friendly.
1) GET methodCreate an OLE object using the sp_OACreate procedure. Pass the created OLE object and make an HTTP request call. Handle the response received from API. Parse the JSON records and insert/ update in the desired table.
In this blog, you will learn about how to call a Web Service from SQL Server, using SQL stored procedure. In my previous project, I was asked to call Web Services from SQL Server stored procedures. It was done using SQL CLR. By using CLR, we can run and manage the code inside the SQL Server.
What is SQL API? CARTO's SQL API allows you to interact with your tables and data inside CARTO, as if you were running SQL statements against a normal database. You can use the SQL API to insert, update or delete data, or to select data from public tables in order to use it on your website or application.
I got another answer as well. I created procedure like follows
CREATE procedure HTTP_Request( @sUrl varchar(200)) As Declare @obj int ,@hr int ,@msg varchar(255) exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT if @hr <> 0 begin Raiserror('sp_OACreate MSXML2.ServerXMLHttp.3.0 failed', 16,1) return end exec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, false if @hr <>0 begin set @msg = 'sp_OAMethod Open failed' goto eh end exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded' if @hr <>0 begin set @msg = 'sp_OAMethod setRequestHeader failed' goto eh end exec @hr = sp_OAMethod @obj, send, NULL, '' if @hr <>0 begin set @msg = 'sp_OAMethod Send failed' goto eh end exec @hr = sp_OADestroy @obj return eh: exec @hr = sp_OADestroy @obj Raiserror(@msg, 16, 1) return GO
I called the stored procedure with url
USE [master] GO DECLARE @return_value int EXEC @return_value = [dbo].[HTTP_Request] @sUrl = N'url' SELECT 'Return Value' = @return_value GO
Thank you guys to make me work this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With