首页 > 解决方案 > Excel VBA graphQL http 请求“运行时错误 -2147012744 服务器返回了无效或无法识别的响应”

问题描述

我编写了 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

标签: excelvbaapihttp-headershttprequest

解决方案


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

`$ 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`

推荐阅读