T-SQL - ODSOLE 扩展过程为此字符串或二进制列或参数指定的源数据太长

问题描述 投票:0回答:2

我的目标是连接到 webapi 并请求数据。

我非常确定我已连接并且正在取回数据,因为错误消息表明有太多数据需要存储。 如果我的 @url + @respondsbody 设置正确,我应该不会得到超过 2500 个字符。

错误:

有人明白我为什么失败吗?

    DECLARE 
    @object int,
    @hr int,
    @src varchar(8000),
    @desc varchar(8000),
    @authHeader NVARCHAR(4000),
    @contentType NVARCHAR(4000),
    @postData NVARCHAR(2000),
    @responseText NVARCHAR(4000),
    @responseXML NVARCHAR(4000),
    @ret INT,
    @status NVARCHAR(4000),
    @statusText NVARCHAR(4000),
    @token INT,
    @methodName varchar(50) = 'GET',
    @requestBody varchar(8000) = '$select=*&limit=1',
    @UserName nvarchar(100) = 'dummy',
    @Password nvarchar(100) = 'dummy' 
DECLARE 
    @url NVARCHAR(4000) = 'https://restapi.dummy.com/prod/rest/api/v1/01/products';
DECLARE
    @json AS TABLE(Json_Table NVARCHAR(4000));



SET NOCOUNT ON
IF    @methodName = ''
    BEGIN
        select FailPoint = 'Method Name must be set'
        return
    END

-- Set Authentications
SET @authHeader = 'BASIC 123456789';
SET @contentType = 'application/json';

-- Open a connection
EXEC @ret = sp_OACreate 'MSXML2.ServerXMLHTTP', @token OUT;
    IF @ret <> 0 
        BEGIN  
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT   
            RAISERROR('Error Creating COM Component 0x%x, %s, %s',16,1, @hr, @src, @desc)  
            RETURN  
        END;  

-- make a request
EXEC @ret = sp_OAMethod @token, 'open', NULL, @methodName, @url, 'false', @UserName, @Password;
    IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'Open failed',
                MethodName = @methodName 
            goto destroy 
            return
        END

EXEC @ret = sp_OAMethod @token, 'setRequestHeader', NULL, 'Authentication', @authHeader;
    IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'SetRequestHeader failed: Authentication',
                MethodName = @methodName 
            goto destroy 
            return
        END

EXEC @ret = sp_OAMethod @token, 'setRequestHeader', NULL, 'Content-type', @contentType;
    IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'SetRequestHeader failed: Content-type',
                MethodName = @methodName 
            goto destroy 
            return
        END

DECLARE
    @len int
    SET @len = len(@requestBody);

EXEC @ret = sp_OAMethod @token, 'setRequestHeader', null, 'Content-Length', @len;
IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'SetRequestHeader failed: Content-Length',
                MethodName = @methodName 
            goto destroy 
            return
        END

EXEC @ret = sp_OAMethod @token, 'send', null, @requestBody
    IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'Send failed',
                MethodName = @methodName 
            goto destroy 
            return
        END

-- Handle responce
EXEC @ret = sp_OAGetProperty @token, 'status', @status OUT;
EXEC @ret = sp_OAGetProperty @token, 'statusText', @statusText OUT;
EXEC @ret = sp_OAGetProperty @token, 'responseText', @responseText OUT;
    IF @ret <> 0 
        BEGIN
            EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
            SELECT      hResult = convert(varbinary(4), @ret), 
                source = @src,
                description = @desc,
                FailPoint = 'ResponseText failed',
                MethodName = @methodName 
            goto destroy 
            return
        END

-- Print response
--PRINT 'Status: ' + @status + ' (' + @statusText + ')';
--PRINT 'Response text: ' + @responseText;

-- Grab the responseText property, and insert the JSON string into a table temporarily. This is very important, if you don't do this step you'll run into problems.
INSERT into @json (Json_Table) EXEC sp_OAGetProperty @token, 'responseText'
--Select the JSON string from the Table we just inserted it into. You'll also be able to see the entire string with this statement.
SELECT * FROM @json

DESTROY: 

EXEC sp_OADestroy @token 

SET NOCOUNT OFF
json sql-server api http-authentication
2个回答
0
投票

将数据类型变量从 4000 更改为 max 是第一步。

最后一击是注释掉下面的 IF 语句。 也许有人可以启发我为什么这个声明没有按预期工作。

EXEC @ret = sp_OAGetProperty @token, 'responseText', @responseText OUT;
--IF @ret <> 0 
    --BEGIN
    --    EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
    --    SELECT      hResult = convert(varbinary(4), @ret), 
    --        source = @src,
    --        description = @desc,
    --        FailPoint = 'ResponseText failed',
    --        MethodName = @methodName 
    --    goto destroy 
    --    return
    --END

0
投票

我也遇到这个问题了。没有解决方案可以解决此问题,但通过使用以下代码,您可以获得所有响应文本。

`EXEC @ret3 = sp_OAGetProperty @internalToken, 'responseText';

如果@ret3 <> 0
开始
将 @src 声明为 VARCHAR(100) 将 @desc 声明为 VARCHAR(200) EXEC sp_OAGetErrorInfo @internalToken、@src OUT、@desc OUT 选择@src + '-' + @desc 结束`

© www.soinside.com 2019 - 2024. All rights reserved.