Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

big help on rest api 2

Status
Not open for further replies.
I've definitely provided REST API use before for you (for Bing maps, as I recall), so you should have a basic grasp of that. What have you tried so far, and where are you stuck?

You'll also need to brush up on the JSON handling I also previous provided examples for, as only seems to return json
 
No code, impossible to understand to make a vb6 code, with i see in the website openapi.it.
is incomprehensible for me.

make an example, please.

for json, no prob you just have illuminated me!

as usual tks.
 
Code:
[COLOR=blue]Private Sub RestExample()
    Dim APICall As String
    Dim Query As String
    Dim strKey As String
    Dim myXML As New MSXML2.DOMDocument60
    Dim nodes As IXMLDOMSelection

    APICall = "[URL unfurl="true"]https://test.cap.openapi.it/cerca_comuni"[/URL] 
    Query = "?comune=Roma" [COLOR=green]' use Rome as an example[/color] 
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", APICall & Query, False
        .setRequestHeader "Authorization", "Bearer <your_token_goes_here>" 
        .send
        JSONDecode Replace(.responseText, "data", "info") [COLOR=green]' avoid the trap that data is a reserved word in VB[/color]
    End With
End Sub

Sub JSONDecode(JSONString)
    Dim oJSON, mydata
    With CreateObject("ScriptControl")
        .Language = "JScript"
        Set oJSON = .Eval("(" + JSONString + ")")
        
        For Each mydata In oJSON.info.result
            Debug.Print "Comune: " & mydata.comune & ", " & mydata.istat
        Next
    End With

End Sub[/color]
 
<sigh> Look, note the URL I am using: it is for the test environment, the sandbox, where I didn't have to pay for an access key. Your tokens are all for the live environment, in which case change the URL for APICall to :
By the way, you might want to edit your post to hide your live token ...
 
i'm on iphone, is my other account.
bro the code now work great!

curiosity

but instead to use Roma as where in query, possibile to use a * to have all for all city?
 
>possibile to use a * to have all for all city?

You've got the code, you could always try it yourself rather than getting someone else to do it for you ... and then ask an appropriate question depending on what you discover.
 
This is the output of Debug.Print .responseText

{
"data": {
"AG": "Agrigento",
"AL": "Alessandria",
"AN": "Ancona",
"AO": "Valle d'Aosta\/Vall\u00e9e d'Aoste",
"AP": "Ascoli Piceno",
"AQ": "L'Aquila",
"AR": "Arezzo",
"AT": "Asti",
"AV": "Avellino",
"BA": "Bari",
"BG": "Bergamo",
"BI": "Biella",
"BL": "Belluno",
"BN": "Benevento",
"BO": "Bologna",
"BR": "Brindisi",
"BS": "Brescia",
"BT": "Barletta-Andria-Trani",
"BZ": "Bolzano\/Bozen",
"CA": "Cagliari",
"CB": "Campobasso",
"CE": "Caserta",
"CH": "Chieti",
"CL": "Caltanissetta",
"CN": "Cuneo",
"CO": "Como",
"CR": "Cremona",
"CS": "Cosenza",
"CT": "Catania",
"CZ": "Catanzaro",
"EN": "Enna",
"FC": "Forl\u00ec-Cesena",
"FE": "Ferrara",
"FG": "Foggia",
"FI": "Firenze",
"FM": "Fermo",
"FR": "Frosinone",
"GE": "Genova",
"GO": "Gorizia",
"GR": "Grosseto",
"IM": "Imperia",
"IS": "Isernia",
"KR": "Crotone",
"LC": "Lecco",
"LE": "Lecce",
"LI": "Livorno",
"LO": "Lodi",
"LT": "Latina",
"LU": "Lucca",
"MB": "Monza e della Brianza",
"MC": "Macerata",
"ME": "Messina",
"MI": "Milano",
"MN": "Mantova",
"MO": "Modena",
"MS": "Massa-Carrara",
"MT": "Matera",
"NA": "Napoli",
"NO": "Novara",
"NU": "Nuoro",
"OR": "Oristano",
"PA": "Palermo",
"PC": "Piacenza",
"PD": "Padova",
"PE": "Pescara",
"PG": "Perugia",
"PI": "Pisa",
"PN": "Pordenone",
"PO": "Prato",
"PR": "Parma",
"PT": "Pistoia",
"PU": "Pesaro e Urbino",
"PV": "Pavia",
"PZ": "Potenza",
"RA": "Ravenna",
"RC": "Reggio Calabria",
"RE": "Reggio nell'Emilia",
"RG": "Ragusa",
"RI": "Rieti",
"RM": "Roma",
"RN": "Rimini",
"RO": "Rovigo",
"SA": "Salerno",
"SI": "Siena",
"SO": "Sondrio",
"SP": "La Spezia",
"SR": "Siracusa",
"SS": "Sassari",
"SU": "Sud Sardegna",
"SV": "Savona",
"TA": "Taranto",
"TE": "Teramo",
"TN": "Trento",
"TO": "Torino",
"TP": "Trapani",
"TR": "Terni",
"TS": "Trieste",
"TV": "Treviso",
"UD": "Udine",
"VA": "Varese",
"VB": "Verbano-Cusio-Ossola",
"VC": "Vercelli",
"VE": "Venezia",
"VI": "Vicenza",
"VR": "Verona",
"VT": "Viterbo",
"VV": "Vibo Valentia"
},
"message": "107 Risultati",
"success": true,
"error": null
}

why have error in:

For Each mydata In oJSON.info.result

 
 https://files.engineering.com/getfile.aspx?folder=2a163a93-6f8e-45a0-b7fc-b1f1fc1d3a48&file=Immagine.jpg
Because you are making a different REST API call with a different result set that my code is not designed to work with.

And, perhaps more surprisingly, the result set you are getting is much more difficult for VB to deal with using the basic jscript tricks I utilise.

You may be better off getting hold of a JSON library for VB if you are going to want to do a lot of JSON work.
 
ok strongm,
but is possible to save the result on a txt file of .responseText, and loop it, for example, with the simple line input?
 
HOW ABOUT:
...
Dim handle As Integer
handle = FreeFile
Open "C:\TABULATI\PROVINCE.txt" For Output As #handle
Print #handle, .responseText
Close #handle
...
 
>loop it, for example, with the simple line input?

Seems like you are hoping that by writing to the text file, you can then somehow parse that back into VB, nicely decoded (i.e you don't really want a file full of JSON, it is just a route to trying to decode it, essentially through string manipulation. But you can do that without using an intermediary file.

Can you clarify your goal?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top