Excel VBA graphQL http请求“运行时错误-2147012744服务器返回无效或无法识别的响应”

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

我已经编写了 VBA 代码来通过 GraphQL API 检索 JSON 结果,但遇到运行时错误

-2147012744 服务器返回无效或无法识别的响应

我的 VBA 代码如下,我在 .send reqQueryAsBody 语句中遇到错误

API文档

Dim req As Object
Dim reqURL As String
Dim BearerToken As String
Dim JsonResult As String

'Declare Query related Dimensions
Dim reqQueryName As String
Dim reqvarName As String
Dim reqQueryAsBody As String

'Declare Json Dimensions
Dim JSONa, element, var As Object, rep As Variant, e As Variant, i As Variant

    Dim sht As Worksheet
    Set sht = ThisWorkbook.Worksheets("Main")
              
    'To check whether Query is Typed or Not in provided space in Main Excel Sheet
    If IsEmpty(sht.Range("reqQuery").value) = True Then
        MsgBox "Kindly Type requested Query in Provided Space in Main Sheet", vbInformation, "Info"
        sht.Range("reqQuery").Activate
        Exit Sub
    
    ElseIf IsEmpty(sht.Range("reqVariable").value) = True Then
        MsgBox "Kindly Select requested variable in Provided Space in Main Sheet", vbInformation, "Info"
        sht.Range("reqVariable").Activate
        Exit Sub
        
    Else

        reqQueryName = sht.Range("reqQuery").value
        reqvarName = sht.Range("reqVariable").value
        BearerToken = "xxxxxxxxxxxx" 
        
        'html URL
        reqURL = "http://tddrapi.trustfci.com/graphql"
        
        Query Prepare Query as per Structure
        reqQueryAsBody = "{""query"": { " & reqQueryName & "" & vbCrLf & _
                    "   (" & vbCrLf & "        offset:0, " & vbCrLf & _
                    "        orderby: """ & reqvarName & """, " & vbCrLf & _
                    "        order: ""asc"" " & vbCrLf & _
                    "   ) " & vbCrLf & _
                    "       {" & vbCrLf & _
                    "           " & reqvarName & " " & vbCrLf & _
                    "       }" & vbCrLf & _
                    "  }" & vbCrLf & "}"

        Debug.Print reqQueryAsBody

        Set req = CreateObject("WinHttp.WinHttpRequest.5.1")

        With req
            .Open "POST", reqURL, False
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "Authorization", "Bearer " & BearerToken
            .send reqQueryAsBody
        End With

        debug.print req.responseText
        JsonResult = req.responseText
excel vba api http-headers httprequest
2个回答
0
投票

首先,服务停止了,不是吗。

`$ ping tddrapi.trustfci.com

PING tddrapi.trustfci.com (70.141.38.184) 56(84) bytes of data.
^C

--- tddrapi.trustfci.com ping statistics ---
8 packets transmitted, 0 received, 100% packet loss, time 7248ms`

0
投票

您是否在没有 vbCrLf 的情况下连接变量“reqQueryAsBody”?

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