Search code examples
jsonexcelvbaxmlhttprequest

Get JSON response XMLHTTP alternative


I have a working code in which I used XMLHTTP API request This is the code

Const sURL As String = "https://developers.zomato.com/api/v2.1/restaurant?res_id=258"
Dim http As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument, json As Object, r As Long


Set http = New MSXML2.XMLHTTP60
Set html = New MSHTML.HTMLDocument
r = 2

With http
    .Open "Get", sURL, False
    .setRequestHeader "User-Agent", "Mozilla/5.0"
    .setRequestHeader "user-key", "APIKEY"
    .send
    html.body.innerHTML = .responseText

    Set json = JSONConverter.ParseJson(.responseText)

Is there an alternative to get the JSON response without using the XMLHTTP library (as this doesn't work on MAC)? Thanks advanced for help.


Solution

  • I have been able to configure it and make it work

    Sub Test()
    Const sUrl As String = "https://reqres.in/api/users/2"
    Dim Response As New WebRequest, Client As New WebClient, rObj As Object, json As Object
    
    Client.BaseUrl = sUrl
    
    With Response
        .Method = WebMethod.HttpGet
        .RequestFormat = WebFormat.json
    
    End With
    
    Set rObj = Client.Execute(Response)
    Set json = WebHelpers.ParseJson(rObj.Content)
    
    Debug.Print rObj.Content
    Debug.Print json("data")("email")
    End Sub