Is it possible to get Response Headers via sp_oamethod? - xmlhttprequest

I have a web service in SQL Server 2014. I'm using sp_oamethod for request to API. Now I'm getting response-code and response-body. And I need to get response header. Can anyone help me?

The following works for me to retrieve Rate-Limit-Remaining from the response header
DECLARE #responseHeader varchar(4000)
EXEC sp_OAMethod #res, 'getResponseHeader', #responseHeader OUT, 'Rate-Limit-Remaining'
PRINT 'Rate-Limit-Remaining: ' + coalesce(#responseHeader,'<null>')
EXEC sp_OAMethod #res, 'getResponseHeader', #responseHeader OUT, 'Rate-Limit-Reset'
PRINT 'Rate-Limit-Reset: ' + coalesce(#responseHeader,'<null>')
EXEC sp_OAMethod #res, 'getAllResponseHeaders', #responseHeader OUT
PRINT 'Response Headers: ' + coalesce(#responseHeader,'<null>')

Related

bcp outfile not found

I'm trying to run the script below, but it returns null. When I run the DOS command, it generates the file normally.
DECLARE #str VARCHAR(1000)
SET #str = 'bcp "Select * FROM WDG.dbo.Facilidade" queryout "w:\xyzTable.txt" -S "WDG-NOTE24\MSSQLWDG" -T -c -t ; '
EXEC xp_cmdshell #str
GO
I need to return a separate txt file for ';' with query data
Tanks
Given your error in your comments i will follow this link and as i described earlier its an access problem to your folder.
Remember it should be given to your Sql service account user and not your self
BCP unable to open BCP host access
I managed to make it work, I found the command below that tests if it has access to the directory.
For some reason did not accept the old path, so I created another one on another disk and it worked.
EXEC master..xp_cmdshell 'DIR C:\sql'
Very thank's for help.
I have a problem, at the end of my import file, it comes with the text '--END--', and when the bulk insert is going to render, it displays an unexpected end message.
I can put some parameter so that when it finds the text it finishes the import.
declare #sql varchar(max)
set #sql = 'BULK INSERT Temp_Facilite FROM ''' + ##FullPath + '''WITH (FIRSTROW = 2,CODEPAGE = ''RAW'',FIELDTERMINATOR = '';'',ROWTERMINATOR = ''0x0A'',MAXERRORS = 3, KEEPNULLS );'
exec (#sql)

How can I format my string in SQL so that it is interpreted properly in Powershell?

I'm currently working on a simple SQL Script that I want to run from SSMS. What the script is supposed to do is take a database, make a backup of it, and then make a .zip file of that backup.
The problem I am running into is when attempting to zip up the backup. I declared all of my variables at the beginning of the file and I believe that when I attempt to execute #sqlcmd the string is not being read by Powershell properly.
SET #sqlcmd = ('powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("' + #bkpath + '", "C:\'+ #fileName + '.zip")')
PRINT #sqlcmd
-- this statement returns (powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip"))
EXEC xp_cmdshell #sqlcmd
Running this code returns the following output in my results window:
At line:1 char:47
+ [IO.Compression.ZipFile]::CreateFromDirectory(C:\Backup", C:\Scriptin ...
+ ~
Missing ')' in method call.
At line:1 char:56
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At line:1 char:47
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\Backup", C:\ScriptingTestDB_20170607.zip)' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
NULL
After some testing I think I figured out that the problem lies with how Powershell interprets the SQL string. Because of the many quotes It's possible that even though SQL prints out the string correctly, when Powershell gets ahold of it the format gets messed up so something needs to be changed there, but still not sure what. Does anyone know of any guidelines I can use to find out how the SQL code should be written such that Powershell reads the following:
[IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip")
You need to escape "-characters, with \. My working script:
declare #sqlcmd varchar(1000), #bkpath nvarchar(max) = 'C:\i1\tmp', #filename nvarchar(max) = 'file'
SET #sqlcmd = 'powershell.exe Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::CreateFromDirectory(\"' + #bkpath + '\", \"C:\i1\'+ #fileName + '.zip\")'
EXEC xp_cmdshell #sqlcmd

SQL Image Extraction

I'm trying to extract images from a restored database. The script below ostensibly runs fine: meaning it extracts the images and saves the number of files I expect to see (and the size I would expect to see) into a network folder as .pdf files. However, when I try to open the files this is the error message I receive:
Adobe Reader could not open 'filename.pdf' because it is either not a supported file type or because the file
has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
I have run this script before in other environments (2 to be exact) and it's worked fine.
I have verified that the actual images are good. You can access the files via the front end.
It doesn't appear that the values are encrypted. Although I'm not completely sure.
The Data Type natively is: image
I've been using SQL for a while but would consider myself a noob, whereas, I'm basically self-taught -so please go easy.
Any guidance / suggestions / insight will be very much welcome.
Thank you.
--========================================================================
USE <<ENTER_DATABASENAME>>
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1
GO
RECONFIGURE
GO
DECLARE #FILEID VARCHAR(12),
#IMAGE VARBINARY(MAX),
#OBJSTREAM INT,
#FILENAME VARCHAR(MAX),
#BASEDIR VARCHAR(MAX)
SET #BASEDIR = '<<ENTER_NETWORKPATH>>'
PRINT 'BASEDIR: ' + #BASEDIR
DECLARE IMGEXTRACT CURSOR FAST_FORWARD FOR
select <<ENTER_COLUMNNAME01>>, <<ENTER_COLUMNNAME02>>
from <<ENTER_TABLENAME>>
OPEN IMGEXTRACT
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
WHILE ##FETCH_STATUS = 0
BEGIN
SET #FILENAME = #BASEDIR + '\' + #FILEID + '.pdf'
PRINT #FILENAME
EXEC sp_OACreate 'ADODB.Stream', #OBJSTREAM OUTPUT
EXEC sp_OASetProperty #OBJSTREAM, 'Type', 1
EXEC sp_OAMethod #OBJSTREAM, 'Open'
EXEC sp_OAMethod #OBJSTREAM, 'Write', NULL, #IMAGE
EXEC sp_OAMethod #OBJSTREAM, 'SaveToFile', NULL, #FILENAME, 2
EXEC sp_OAMethod #OBJSTREAM, 'Close'
EXEC sp_OADestroy #OBJSTREAM
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
END
CLOSE IMGEXTRACT
DEALLOCATE IMGEXTRACT
GO
--========================================================================

Getting 0x800C0005 System error in SQL Server

The following code is used to call a web service from SQL Server:
Declare #Object as Int;
Declare #ResponseText as VARCHAR(MAX);
Exec sp_OACreate 'MSXML2.XMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'GET','http://localhost/Service1.asmx/HelloWorld?name=xyz', false;
Exec sp_OAMethod #Object, 'send';
EXEC sp_OAGetErrorInfo #Object
Create table #tmp(dt varchar(max))
insert into #tmp
exec sp_OAGetProperty #Object, 'ResponseText' --,#strLine OUtPUT
Select dt from #tmp -- single column/single row.
Drop Table #tmp -- clean up
Exec sp_OADestroy #Object;
But I am getting below error:
0x800C0005 msxml3.dll System error: -2146697211
Any pointers ...?
There are two (2) ways to fix Msxml3 Dll 0x800c0005 System Error Error:
Advanced Computer User Solution (manual update):
1) Start your computer and log on as an administrator.
2) Click the Start button then select All Programs, Accessories, System Tools, and then click System Restore.
3) In the new window, select "Restore my computer to an earlier time" option and then click Next.
4) Select the most recent system restore point from the "On this list, click a restore point" list, and then click Next.
5) Click Next on the confirmation window.
6) Restarts the computer when the restoration is finished.
Novice Computer User Solution (completely automated):
1) Download (Msxml3 Dll 0x800c0005 System Error) repair utility.
2) Install program and click Scan button.
3) Click the Fix Errors button when scan is completed.
4) Restart your computer.
More details

Call web service from stored procedure - special characters aren't shown correctly

I'm trying to call a web service from a stored procedure in SQL Server 2012:
DECLARE #ResponseText AS VARCHAR(8000)
DECLARE #Object AS INT;
DECLARE #idoc INT;
Exec sp_OACreate 'MSXML2.ServerXMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'get', #Webservice, 'false'
Exec sp_OAMethod #Object, 'send'
Exec sp_OAMethod #Object, 'responseText', #ResponseText OUTPUT
Exec sp_OADestroy #Object
EXEC sp_xml_preparedocument #idoc OUTPUT, #ResponseText
The call works but the result string #ResponseText won't convert some special characters. An ë (with umlaut) turns into ? and the next character is cut of. The ë is also shown as ë in the source of the xml, so it isn't translated to ë
The < and &characters as are translated well (into < and &) and these give no problems.
The returning XML is from the <?xml version="1.0" encoding="ISO-8859-1"?> format and it displays correctly when I open the service in a browser.
Can I do anything to make the ë work as well or do I need to change my returning xml and change the ë to ë?
Use NVARCHAR type to resolve this issue.
An ë (with umlaut) turns into ?
You might need a user function like this to encode the resulting string to HTML for this one
the next character is cut of, < turns into <, & into &... and
so on