Post JSON to web in excel vba

后端 未结 1 1832
轻奢々
轻奢々 2021-01-04 07:29

I want to POST some JSON with some VBA:

Dim sURL As String, sHTML As String, sAllPosts As String
Dim oHttp As Object
Dim blWSExists As Boolean
Set oHttp = Cr         


        
相关标签:
1条回答
  • 2021-01-04 07:59

    JSON can be very sensitive to how it's formatted, so I would make sure everything is quoted properly before it is sent. I would recommend splitting Body into a separate variable and debugging the value with http://jsonformatter.curiousconcept.com/ before sending.

    Dim Body As String
    Body = "{""mType"":""OPEN_SYSTEM_TRADE"",""systemOwnerId"":10}"
    
    ' Set breakpoint here, get the Body value, and check with JSON validator
    oHttp.Send Body
    

    I ran into many similar issues when working with Salesforce's REST API and combined my work into a library that may be useful to you: https://github.com/VBA-tools/VBA-Web. Using this library, your example would look like:

    Dim Body As New Dictionary
    Body.Add "mType", "OPEN_SYSTEM_TRADE"
    Body.Add "systemOwnerId", 10
    
    Dim Client As New WebClient
    Dim Response As WebResponse
    Set Response = Client.PostJson("somewebsite.com", Body)
    
    Worksheets("Open1").Range("A1").Value = Response.Content
    
    0 讨论(0)
提交回复
热议问题