Communication with NAVision with SOAP API from SQL - sql-server

I have a problem with GET method from API. So, I had some code for body for GET method, and when I copy this code to postman works fine.
set #requestBody = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ReadMultiple xmlns="urn:microsoft-dynamics-schemas/page/rfidtransferlines">
<filter>
<Field>Document_No</Field>
<Criteria>PN21-001126</Criteria>
</filter>
</ReadMultiple>
</s:Body>
</s:Envelope>'
print #requestBody
Now, I want to get data from API, and I have code for GET method:
-- Open the connection.
EXEC sp_OACreate 'MSXML2.ServerXMLHTTP', #token OUT; --MSXML2.ServerXMLHTTP
IF #ret <> 0 RAISERROR('Unable to open HTTP connection.', 10, 1);
-- Send the request.
EXEC sp_OAMethod #token, 'open', NULL, 'GET', #url, 'false',#username,#password;
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'Connection', 'keep-alive';
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'SOAPAction', 'ReadMultiple';
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'Content-type', #contentType;
EXEC sp_OAMethod #token, 'send',NULL,#requestBody;
-- Handle the response.
EXEC sp_OAGetProperty #token, 'status', #statusCode OUT;
EXEC sp_OAGetProperty #token, 'statusText', #statusText OUT;
EXEC sp_OAGetProperty #token, 'responseText', #responseText OUT;
-- Show the response.
PRINT 'Status: ' + #statusCode + ' (' + #statusText + ')';
PRINT 'Response text: ' + #responseText;
I think that problem is here :
EXEC sp_OAMethod #token, 'send',NULL,#requestBody;
or maybe my variable:
DECLARE #responseText NVARCHAR(4000);
is set to 4000 characters, because is max for SQL Server.
Can somebody help me.

Related

Call formsite API from SQL Server

I am trying to call Formsite API from SQL Server to get the data but I am unable to get the data it is returning me null, below is the script which I am using to get the data.
This is my API link (dummy link):
https://fs29.formsite.com/api/v2/xyz/forms/form14/
Access Token (also dummy):
myaccesskey
And the script which I am using is
DECLARE #authHeader NVARCHAR(64);
DECLARE #contentType NVARCHAR(64);
DECLARE #postData NVARCHAR(MAX);
DECLARE #responseText NVARCHAR(MAX);
DECLARE #responseXML NVARCHAR(MAX);
DECLARE #ret INT;
DECLARE #status NVARCHAR(32);
DECLARE #statusText NVARCHAR(32);
DECLARE #token INT;
DECLARE #url NVARCHAR(256);
-- Set Authentications
SET #authHeader = 'Bearer **mykey**';
SET #contentType = 'application/json';
SET #url = 'https://fs29.formsite.com/api/v2/GwYV/forms/form14/' -- Dummy api url
EXEC #ret = sp_OACreate 'MSXML2.XMLHTTP', #token OUT;
SELECT #token
IF #ret <> 0
RAISERROR('Unable to open HTTP connection.', 10, 1);
-- build a request
EXEC #ret = sp_OAMethod #token, 'open', NULL, 'GET', #url, 'false';
EXEC #ret = sp_OAMethod #token, 'setRequestHeader', NULL, 'Authorization', #authHeader;
EXEC #ret = sp_OAMethod #token, 'setRequestHeader', NULL, 'Content-type', #contentType;
EXEC #ret = sp_OAMethod #token, 'setRequestHeader', NULL, 'Cache-Control', 'no-cache' ;
EXEC #ret = sp_OAMethod #token, 'send'
-- Handle response
EXEC #ret = sp_OAGetProperty #token, 'status', #status OUT;
EXEC #ret = sp_OAGetProperty #token, 'statusText', #statusText OUT;
EXEC #ret = sp_OAGetProperty #token, 'responseText', #responseText OUT;
-- Print responses
SELECT #responseText
PRINT 'Status: ' + #status + ' (' + #statusText + ')';
PRINT 'Response text: ' + #responseText;

Get SOAP web service gzip enconding response from SQL Server

I'm trying to call a SOAP web service from SQL Server using OLE Automation stored procedures, and I've got troubles with the response which is Content-Encoding gzip.
When test the method using SOAP UI works like a charm.
Soap UI shows response like xml. Http log shows response is gzip encoded
When I make the call from SQL Server, I get an unexpected response like this
XML response from SQL Server
Status return is ok 200.
GetAllResponseHeaders doesn't include 'Content-Encoding: gzip', Soap UI does!
I included all request headers showed on SOAP UI.
Here is my code (I omitted error handling for the sake of readability)
DECLARE
#URI varchar(2000) = 'http://foo/foo.wsdl',
#methodName varchar(50) = 'post',
#requestBody varchar(8000) = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CCTwebservices"><soapenv:Header/><soapenv:Body><urn:recepcionLiberacionTatc soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><user xsi:type="xsd:string">foo</user><password xsi:type="xsd:string">foo</password><dataIn xsi:type="xsd:string"><![CDATA[foo]]></dataIn></urn:recepcionLiberacionTatc></soapenv:Body></soapenv:Envelope>',
#SoapAction varchar(255) = 'urn:CCTwebservices#recepcionLiberacionTatc',
#responseText varchar(8000);
DECLARE #objectID int
DECLARE #hResult int
DECLARE #source varchar(255), #desc varchar(255)
--create object
EXEC #hResult = sp_OACreate 'MSXML2.ServerXMLHTTP', #objectID OUT
-- open the destination URI with Specified method
EXEC #hResult = sp_OAMethod #objectID, 'open', null, #methodName, #URI, 'false', null, null
-- set request headers
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml;charset=UTF-8'
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'Accept-Encoding', 'gzip,deflate'
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'Host', 'ws.foo`enter code here`.cl'
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'Connection', 'Keep-Alive'
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'SOAPAction', #SoapAction
declare #len int
set #len = len(#requestBody)
EXEC #hResult = sp_OAMethod #objectID, 'setRequestHeader', null, 'Content-Length', #len
-- send the request
EXEC #hResult = sp_OAMethod #objectID, 'send', null, #requestBody
EXEC #hResult = sp_OAMethod #objectID, 'getResponseHeader', null, 'Content-Encoding' -- Return null
EXEC #hResult = sp_OAMethod #objectID, 'GetAllResponseHeaders' -- Response doesn't incluye 'Content-Encoding: gzip', Soap UI does!.
exec sp_OAGetProperty #objectID, 'StatusText'
exec sp_OAGetProperty #objectID, 'Status'
exec sp_OAGetProperty #objectID, 'responseText' -- return the xml show previosly
exec sp_OADestroy #objectID
Any help to make this work?

Calling API from SQL Server [Issue]

I am working with API and MSSQL. I need to call an API from SQL Server. But On execution, its response is null. Can anyone list errors if some in the queries?
The API responds but the issue is it shows error in Authentication while I'm using exact correct credentials. It's a POST API.
This is the only way I found correct on browsing the whole a day on Google. Can someone help or list errors?
DECLARE #authHeader NVARCHAR(MAX)= 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX'
DECLARE #contentType NVARCHAR(64) = 'application/json; charset=utf-8'
DECLARE #appVersion NVARCHAR(20) = '3.2.1'
DECLARE #appkey NVARCHAR(50) = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
DECLARE #deviceId NVARCHAR(50) = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
DECLARE #deviceType int = 1;
DECLARE #language int = 1;
DECLARE #status NVARCHAR(32);
DECLARE #statusText NVARCHAR(32);
DECLARE #Body as VARCHAR(8000) =
'{
"itemId":"XXXXXXXXXXXXXXXXXXXXXXX",
"itemType":XXXXXXXXXXXXX,
"liked":"true"
}'
DECLARE #responseText NVARCHAR(2000);
DECLARE #token INT;
DECLARE #url NVARCHAR(256) = 'https://api-stage.testapp.com/v4/test';
Exec sp_OACreate 'MSXML2.ServerXMLHTTP', #token OUT;
EXEC sp_OAMethod #token, 'open', NULL, 'POST', #url, 'false'
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'Authentication', #authHeader
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'Content-type', #contentType
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'appVersion', #appVersion
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'appkey', #appkey
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'deviceId', #deviceId
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'devicetype', #deviceType
EXEC sp_OAMethod #token, 'setRequestHeader', NULL, 'language', #language
EXEC sp_OAMethod #token, 'send', null
Exec sp_OAMethod #token, 'setRequestBody', null, 'Body', #body
EXEC sp_OAGetProperty #token, 'status', #status OUT;
EXEC sp_OAGetProperty #token, 'statusText', #statusText OUT;
EXEC sp_OAGetProperty #token, 'responseText', #responseText OUT;
PRINT 'Status: ' + #status + ' (' + #statusText + ')';
PRINT 'Response text: ' + #responseText;
Select #ResponseText as Response

SQL Server Woocommerce API connection gives NULL when getting products, works fine with orders

I am trying to set up a connection between a external SQL Server Database and the Woocommerce API.
The goal is to communicate between 2 websites, where they exchange data, the external website will send a POST request to Woocommerce to create a product, then GET the list of products to select the latest added one and then uses that product ID to create a add to cart link which will put the product into the Cart.
Now here is my code :
DECLARE #contentType NVARCHAR(64);
DECLARE #ret INT;
DECLARE #responseText NVARCHAR(4000);
DECLARE #status NVARCHAR(32);
DECLARE #statusText NVARCHAR(32);
DECLARE #token INT;
DECLARE #url NVARCHAR(256);
SET #contentType = 'application/json';
set #url = 'https://mywebsite.com/wp-json/wc/v3/products/?consumer_key=mykey&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1458225139&oauth_nonce=nVq4rX&consumer_secret=mysecret&oauth_signature=kzoVx%20VYSWlLbRpi3f8222222%3D'
EXEC #ret = sp_OACreate 'WinHttp.WinHttpRequest.5.1', #token OUT;
EXEC #ret = sp_OAMethod #token, 'open', NULL, 'GET', #url, 'false';
EXEC #ret = sp_OAMethod #token, 'setRequestHeader', NULL, 'Content-type', #contentType;
EXEC #ret = sp_OAMethod #token, 'send', NULL;
EXEC #ret = sp_OAGetProperty #token, 'status', #status OUT;
EXEC #ret = sp_OAGetProperty #token, 'statusText', #statusText OUT;
EXEC #ret = sp_OAGetProperty #token, 'responseText', #responseText OUT;
EXEC #ret = sp_OADestroy #token;
RETURN #responseText;
When i return the status i get 200, which to my knowledge is the desirable status.
But when i return responseText i get null.
The infuriating part is when i replace products with orders i get a result as expected, but when getting the products it keeps saying NULL.
Please help, because i am losing the last few hairs on my head.
Also my boss insists on doing it by SQL Server..

SQL Server - Call a REST API using a virtual CSV

I have the code below that makes a request to an API and works fine, using JSON content-type to another endpoint.
DECLARE #Object AS int;
DECLARE #ResponSEText AS Varchar(8000);
DECLARE #Token AS Varchar(8000);
DECLARE #xmltest AS Varchar(8000);
DECLARE #hResult AS int
DECLARE #source varchar(255), #desc varchar(255)
DECLARE #LocationId varchar(25);
DECLARE #Body AS varchar(8000) = '{
"UserName": "test.integration12",
"Password": "Urgent123"
}'
--EXEC sp_OACREATE 'MSXML2.XMLHTTP', #Object OUT;
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', #Object OUT;
EXEC sp_OAMethod #Object, 'open', NULL, 'POST',
'https://urgentcargus.azure-api.net/api/LoginUser', 'false'
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Content-Type', 'application/json'
DECLARE #len int
SET #len = len(#body)
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Ocp-Apim-Subscription-Key','4f82f9d067914287979884f920d86ffb'
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Content-Length', #len
EXEC sp_OAMethod #Object, 'SETRequestBody', null, 'Body', #body
EXEC sp_OAMethod #Object, 'sEND', null, #body
EXEC sp_OAMethod #Object, 'responSEText', #ResponSEText OUTPUT
I would like to adapt the code above in order to send the values of the parameters like a virtual csv file instead of an XML or JSON protocol with the appropriate format to another endpoint using a different content-type and I cannot figure it out how to do this:
DECLARE #Object AS int;
DECLARE #ResponSEText AS Varchar(8000);
DECLARE #Token AS Varchar(8000);
DECLARE #xmltest AS Varchar(8000);
DECLARE #hResult AS int
DECLARE #source varchar(255), #desc varchar(255)
DECLARE #LocationId varchar(25);
DECLARE #Body AS varchar(8000) = -- here should go the parameters which are username, clientid, password and file (where file has to be the virtual CSV file with the columns and rows separated by "|"
EXEC sp_OACREATE 'MSXML2.ServerXMLHttp', #Object OUT;
EXEC sp_OAMethod #Object, 'open', NULL, 'POST',
'https://www.selfawb.ro/import_awb_integrat.php', 'false'
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Content-Type', 'text/xml'
EXEC sp_OAMethod #Object, 'SETRequestHeader', null, 'Ocp-Apim-Trace:true'
EXEC sp_OAMethod #Object, 'SETRequestBody', null, 'Body', #body
EXEC sp_OAMethod #Object, 'sEND', null, #body
EXEC sp_OAMethod #Object, 'responSEText', #ResponSEText OUTPUT
I asked the owner of the API and they provided to me an example as shown in the print screen below:
Example
Does anyone can give me a hint or a hand? Thanks
If you want to send a virtual file, e.g. a file that you generate in memory then send as an attachment as part of the POST, then you would just need include the csv text as part of the post as an attachment with a filename. Try using PostMan to set this up using an actual csv file and analyze the POST to view how to format the csv text to be serialized over the wire as a csv file attachment.
Otherwise if you are using SQL 2016 or above, you can parse csv text using STRING_SPLIT. Hope this helps.
DECLARE #CsvParams NVARCHAR(256) =
'UserName,test.integration12,'
+ 'Password,Urgent123'
SELECT value FROM STRING_SPLIT(#CsvParams, ',');
The code above produces a table with the separated values:
More on STRING_SPLIT here:
STRING_SPLIT (Transact-SQL)

Resources