带curl的Excel VBA JSON POST请求

时间:2014-12-10 11:59:17

标签: json excel vba curl

我正在尝试使用Excel VBA中的JSON发送请求,请求通过但我得到的只是一个空白回复。

以下是示例请求:

https://developers.google.com/qpx-express/v1/requests

    Sub QPX()

    Dim sURL As String
    Dim sJson As String
    Dim xmlDoc As New DOMDocument
    Dim sEnvlength As Integer
    Dim responseText As String

    Dim ApiKey As String
        ApiKey = "MyAPIkey"

    Set ObjHttp = New MSXML2.XMLHTTP
    sURL = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=" & ApiKey

         sJson = sJson & " {"
         sJson = sJson & "   ""request"": {"
         sJson = sJson & "   ""slice"": ["
         sJson = sJson & "     {"
         sJson = sJson & "        ""origin"": ""LHR"","
         sJson = sJson & "        ""destination"": ""VIE"","
         sJson = sJson & "        ""date"": ""2014-12-24"""
         sJson = sJson & "      }"
         sJson = sJson & "    ],"
         sJson = sJson & "    ""passengers"": {"
         sJson = sJson & "      ""adultCount"": 1,"
         sJson = sJson & "     ""infantInLapCount"": 0,"
         sJson = sJson & "      ""infantInSeatCount"": 0,"
         sJson = sJson & "      ""childCount"": 0,"
         sJson = sJson & "      ""seniorCount"": 0"
         sJson = sJson & "    },"
         sJson = sJson & "    ""solutions"": 20,"
         sJson = sJson & "    ""refundable"": false"
         sJson = sJson & "  }"
         sJson = sJson & "}"


    ObjHttp.Open "POST", sURL, False

    ObjHttp.setRequestHeader "Content-Type", "application/json"


    ObjHttp.send (sJson)

    xmlDoc.LoadXML (ObjHttp.responseText)
    MsgBox responseText

    End Sub

他们的指导说:

然后从与上述request.json文件相同的目录中执行以下命令。 (如先决条件中所述,您必须首先获取API密钥。)

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=your_API_key_here

如何在编码中实现curl?

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

我在一个月前做了类似的研究(发布为答案here)。您可以使用VBA JSON尝试TCP library

Sub main()
  Dim tcp As New TCPClient
  Dim ok As Boolean

  ok = tcp.Connect("192.168.1.63", 6666)
  If Not ok Then
      Debug.Print "Error Connecting"
      Exit Sub
  End If

  tcp.Send "{""Country"":""France"",""Salaries"":{""Peyton Manning"":800.0,""Blaine Gabbert"":200000.0},""Name"":""Amazon.net""}"
  tcp.CloseConnection

End Sub