Calling a REST API from SQL Server

48 Views Asked by At

I am using the following code to call my REST API from SQL Server but I am getting empty string in the response message. I put a breakpoint in visual studio on getTravelerInfoById method but it is also not triggered. Can you please check and let me know what am I doing wrong?

DECLARE @URL NVARCHAR(MAX) = 'https://localhost:7128/api/TravelerBase/getTravelerInfoById';
DECLARE @Object AS INT;
DECLARE @ResponseText AS VARCHAR(8000);
DECLARE @Body AS VARCHAR(8000) =
'{
   "var1": "2024-03-16 16:56:59",
   "var2": "+0200",
   "var3": "856"
}'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'post',
                 @URL,
                 'false'
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Type', 'application/json'
EXEC sp_OAMethod @Object, 'send', null, @body
EXEC sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

SELECT @ResponseText As 'ResponseText'

IF CHARINDEX('false',(SELECT @ResponseText)) > 0
BEGIN
 SELECT @ResponseText As 'Message'
END
ELSE
BEGIN
 SELECT @ResponseText As 'customer Details'
END
EXEC sp_OADestroy @Object

I enabled the procedures as below:

sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'Ole Automation Procedures', 1;  
GO  
RECONFIGURE;  
GO

I tried a GET api https://localhost:7128/api/TravelerBase/getVersion as well but it is also not triggered.

I also tried the following simple API that gives version number but its also giving empty string.

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);

--Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
                 'https://vps-7034f481.vps.ovh.net/simat/simatapi/api/TravelerBase/getVersion', --Your Web Service Url (invoked)
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

Select @ResponseText

Exec sp_OADestroy @Object

Thanks.

0

There are 0 best solutions below