Sample code for 30+ languages & platforms
SQL Server

Demonstrate the REST FullRequestNoBodySb Method

See more REST Examples

Demonstrates the FullRequestNoBodySb method, which sends an HTTP request with no body and receives the response in a Chilkat StringBuilder object.

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 requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Connect to the REST server.
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'chilkatsoft.com', @port, @bTls, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    -- Send an HTTP request with no body...

    -- An HTTP request with no body is a simple HTTP request that is typically a GET or DELETE.
    -- (POST and PUT requests typically have a request body.)
    -- The GET method is used to request data from a specified resource, 
    -- and it does not have a request body. Here's an example of an HTTP GET request:
    -- 
    -- GET /api/data HTTP/1.1
    -- Host: example.com
    -- Accept: application/json
    -- 
    --  - The HTTP method (also known as the verb) is "GET," indicating that the client wants to retrieve data from the specified resource.
    --  - The request path is "/api/data," representing the resource the client wants to access.
    --  - The "Host" header specifies the hostname of the server being requested.  Chilkat automatically adds it.
    --  - The "Accept" header indicates the media type (MIME type) that the client can understand and would like to receive in the response. In this case, it specifies that the client prefers to receive data in JSON format.
    -- 
    -- Since the GET method does not have a request body, the request ends after the headers. 
    -- The server will process the request, retrieve the requested data (if available), and respond with an HTTP response 
    -- containing the requested data (if any) in the message body.
    -- 
    -- The body of the HTTP response is written to the StringBuilder object (overwriting whatever content the StringBuilder may have already contained).
    -- 

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Accept', 'application/json'

    DECLARE @sbJson int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJson OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/testData/helloWorld.json', @sbJson
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbJson
        RETURN
      END


    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    PRINT 'Response status code = ' + @iTmp0

    PRINT 'Response body:'
    EXEC sp_OAMethod @sbJson, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbJson


END
GO