Link to home
Start Free TrialLog in
Avatar of Angel02
Angel02

asked on

JSON Response and request in VB6 application

I am working with a VB6 application. I need to send an HTTP request and the receive JSON response. I then need to deserialize the response. I have done this already in VB.NET web application.

I am wondering if it is possible in VB6 application. If yes, can you please advise? Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What Kind of HTTP request? Good Web API's offer different result formats, e.g. XML.
Avatar of Angel02
Angel02

ASKER

Thank you Gustav. Your code worked perfectly.
Now I am trying to read data from the response into a recordset as needed by my application.

Is it possible to assign data from DataCollection to  a ADODB recordset?

@ste5an
I am currently making GET requests. I later also need to make POST requests.

I used the following in Gustav's code

Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", ServiceUrl, Async
Is it possible to assign data from DataCollection to  a ADODB recordset?

Certainly. You would loop the collection holding the response and, item by item, add a record to your recordset reading the fields of the collection.

/gustav
It's not about GET or POST.

Normally you can specify your result type by using the appropriate accept header. Either accept: application/xml or accept: application/json.
Avatar of Angel02

ASKER

@stef5n Sorry. Get it now. Gustav's code is working good for responses. I would now like to know about sending requests.

@Gustav
Change of plan. I am now sending the jSon response to a table in SQL server. I got that to work.

I do want to know if your code has a way of sending a json request too. Below is a sample request that I need to send.

{
    "securityID": "65gfggw",
    "productId": 3452,
    "quantity": "5"
   }
Sorry, no. As you may have seen, the code just sends a URL with appended parameters, that's all. So there are no efforts to encode data as Json and post these.

I'm sure the Json modules could be expanded to cover this, but I just haven't had the need and, currently, I have no time for working with it, though it could be fun.

/gustav
Well, I located this code here which seems to do exactly what you are after:
    Dim objHTTP As Object
    Dim Json As String
    Json = Range("A15") 'here I am pulling in an existing json string to test it. String is created in other VBA code

    Dim result As String

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URl = "http://myApi/iSendJsonto/"
    objHTTP.Open "POST", URl, False

   objHTTP.setRequestHeader "Content-type", "application/json"
   objHTTP.send (Json)
   result = objHTTP.responseText

   'Some simple debugging
   Range("A25").Value = result
   Range("A26").Value = Json

   Set objHTTP = Nothing

Open in new window

It uses late binding which you could replace with early binding as in my function RetrieveDataResponse and, of course, you will have to replace the use of ranges to collect the response with variables or collections as to your need.

/gustav
I never said, that Gustav's approach is not working or incorrect. I just added a comment, that you maybe don't need to work with XML. Cause good REST services offer multiple answer formats.
Avatar of Angel02

ASKER

@ste5fan I agree
@gustav thank you!