Sample code for 30+ languages & platforms
SQL Server

HTTPS MWS List Orders (Amazon Marketplace Web Service)

See more HTTP Misc Examples

Send an HTTPS MWS ListOrders request to return a list of orders created or updated during a time frame.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 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 HTTP 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

    -- Make sure to connect to the correct Amazon MWS Endpoing, otherwise
    -- you'll get an HTTP 401 response code.
    -- 
    -- The possible servers are:
    -- 
    -- North America (NA) 	https://mws.amazonservices.com
    -- Europe (EU) 	https://mws-eu.amazonservices.com
    -- India (IN) 	https://mws.amazonservices.in
    -- China (CN) 	https://mws.amazonservices.com.cn
    -- Japan (JP) 	https://mws.amazonservices.jp 
    -- 

    -- Build the HTTP request.
    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    -- Add query params
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'Action', 'ListOrders'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'CreatedAfter', '2016-12-31T23:00:00Z'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'MarketplaceId.Id.1', 'MWS_MARKETPLACE_ID'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'SellerId', 'MWS_SELLER_ID'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'AWSAccessKeyId', 'MWS_ACCESS_KEY_ID'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'SignatureVersion', '2'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'SignatureMethod', 'HmacSHA256'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'Version', '2013-09-01'

    -- Set the HTTP verb and path.
    EXEC sp_OASetProperty @req, 'Path', '/Orders/2013-09-01'
    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'

    -- Add the MWS Signature after the verb, path, and all params have been set.
    EXEC sp_OAMethod @req, 'AddMwsSignature', @success OUT, 'mws.amazonservices.com', 'MWS_SECRET_ACCESS_KEY_ID'

    EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://mws.amazonservices.com/Orders/2013-09-01', @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Non-success status code: ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- Examine the XML returned in the response body.
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----'

    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp


END
GO