SQL Server
SQL Server
WhatsApp - Download/Retrieve Media
Demonstrates how to download media previously uploaded to the WhatsApp Business API clientChilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Implements the following CURL command:
-- curl -X GET \
-- https://your-webapp-hostname:your-webapp-port/v1/media/4b5bf27b-8672-4d55-bc21-d096dc200d0f \
-- -H 'Authorization: Bearer your-auth-token' \
-- -o path/filename
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
-- Adds the "Authorization: Bearer your-auth-token" header.
EXEC sp_OASetProperty @http, 'AuthToken', 'your-auth-token'
DECLARE @bdResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdResponseBody OUT
EXEC sp_OAMethod @http, 'QuickGetBd', @success OUT, 'https://your-webapp-hostname:your-webapp-port/v1/media/4b5bf27b-8672-4d55-bc21-d096dc200d0f', @bdResponseBody
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @bdResponseBody
RETURN
END
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT
PRINT 'response status code = ' + @respStatusCode
EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT
PRINT 'response header = ' + @sTmp0
IF @respStatusCode <> 200
BEGIN
PRINT 'Error response body:'
EXEC sp_OAMethod @bdResponseBody, 'GetString', @sTmp0 OUT, 'utf-8'
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @bdResponseBody
RETURN
END
EXEC sp_OAMethod @bdResponseBody, 'WriteFile', @success OUT, 'path/filename'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @bdResponseBody
END
GO