Api Key method post in VBA


#1

Hi I’m civil engineer and mostly I’m using excel. As I work with bill of quantities I often use maps for transportation distances etc. I want to avoid manually typing so I tried to use Api key v2 but I’m stuck. When I run code bellow I get an error. I’ve made function with Apy key V1 (get method) and it’s working just fine but only limitation is that distances can’t exceed 300,00 km.

Please can you check code for post method because I don’t know what I’m doing wrong.

Thank you

Sub proba()

Dim objHTTP As WinHttp.WinHttpRequest
Dim URL As String
Dim DataReq As String
Dim Parametri As String
Dim ApiKey As String

Dim Lokacija As String
Dim Odrediste As String

ApiKey = Sheet3.Range(“B2”).Value
Lokacija = Sheet3.Range(“B7”).Value
Odrediste = Sheet3.Range(“B11”).Value

URL = “https://api.openrouteservice.org/v2/directions/driving-car/json
Parametri = “{” & Chr(34) & “coordinates” & Chr(34) & “:[[” & Lokacija & Odrediste & “]]})”

ParByte = StrConv(Parametri, vbUnicode)

Set objHTTP = New WinHttp.WinHttpRequest
With objHTTP
.Open “POST”, URL, False
.setRequestHeader “Authorization”, ApiKey
.send ParByte
End With

DataReq = objHTTP.responseText
Debug.Print DataReq

End Sub


#2

Hi @bkrizan,

I have no idea what language that should be, but you might be missing some other important Headers if they are not set by default.
https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/json/post
Take a look at the example code (left of the call button)

best regards