1

What is this api code's VBA equivalent (for Name.com Domain Check API):

curl -u 'username:token' 'https://api.dev.name.com/v4/domains:checkAvailability' -X POST --data '{"domainNames":["example.org"]}'

Tried:

Set request = CreateObject("WinHttp.WinHttpRequest.5.1") '
url = "https://api.dev.name.com/v4/domains:checkAvailability"
request.Open "POST", url, False
request.setRequestHeader "username:token", "???:???"
or 
request.SetCredentials "username", "token", 0
request.setRequestHeader "domainNames", Range("C1").Value & ".com"
request.Send
MsgBox request.ResponseText

It says: "message:","unauthenticated"

Note: for GoDaddy Domain Availability API, that conversion works:

curl -X GET -H"Authorization: sso-key [API_KEY]:[API_SECRET]""https://api.godaddy.com/v1/domains/available?domain="

VBA:

url = "https://api.godaddy.com/v1/domains/available?domain=" & Range(CellC).Value & ".com"
request.setRequestHeader "Authorization", "sso-key ???:???"

(username and token/key (???) are hidden)

Also, for "whois.internic.net" query, a (running) VBA code example will be appreciated.

Opt Names
  • 11
  • 1
  • 3

3 Answers3

1

You need to base64 encode the authentication details and pass in headers and pass the list of domains in the body. I have used jsonconverter.bas to parse the json response. After adding the bas you need to add the reference shown below. Also, add reference to Microsoft xml

Public Sub GetResults()
    Dim data As String, json As Object '<  VBE > Tools > References > Microsoft Scripting Runtime
    data = "{""domainNames"":[""google.com""]}"
    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "POST", "https://api.dev.name.com/v4/domains:checkAvailability", False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
        .setRequestHeader "Authorization", "Basic " + _
            EncodeBase64("username" + ":" + "Token")
        .send data
        Set json = JsonConverter.ParseJson(.responseText)
        Dim result As Object
        For Each result In json("results")
            Debug.Print result("domainName")
        Next
    End With
End Sub

Function EncodeBase64(text As String) As String


  Dim arrData() As Byte
  arrData = StrConv(text, vbFromUnicode)

  Dim objXML As MSXML2.DOMDocument60
  Dim objNode As MSXML2.IXMLDOMElement

  Set objXML = New MSXML2.DOMDocument60
  Set objNode = objXML.createElement("b64")

  objNode.DataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64 = Application.Clean(objNode.text)

  Set objNode = Nothing
  Set objXML = Nothing
End Function

base64 function taken from here/here.

If you know python you can more simply do:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}

data = '{"domainNames":["google.com"]}'

result = requests.post('https://api.dev.name.com/v4/domains:checkAvailability', data=data, headers = headers, auth=('username','token')).json()
print(result['results'])
QHarr
  • 83,427
  • 12
  • 54
  • 101
0

Can you try XMLHTTP and see if it makes any difference including username and token in open method?

url = "https://api.dev.name.com/v4/domains:checkAvailability"

Set request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
request.open("POST", url, False, "username", "token")
request.setRequestHeader "domainNames", Range("C1").Value & ".com"
request.Send

MsgBox request.ResponseText
dbmitch
  • 5,361
  • 4
  • 24
  • 38
0

Why not use curl directly (via the windows shell):

For the code below, you set a reference to the Windows Script Host Object Model for early binding.

Of course, I can't test it as given since it returns "permission denied" which, I assume, is due to the invalid username:token.

Note the "" escaped quotes for the quote marks included within the quoted string.

Option Explicit
Sub curl()
    Const strCurl As String = """username:token"" ""https://api.dev.name.com/v4/domains:checkAvailability"" -X POST --data ""{""domainNames"":[""example.org""]}"""
    Dim WSH As WshShell, lErrCode As Long
    Dim FSO As FileSystemObject, TS As TextStream
    Dim sTemp As String
    Dim sBasePath As String
    Dim I As Long
    Dim strJson As String

    sTemp = Environ("Temp") & "\FileList.txt"



Set WSH = New WshShell
lErrCode = WSH.Run("CMD /c curl -u" & strCurl & " > " & sTemp, xlHidden, True)

'lErrCode = WSH.Run("CMD /c tree """ & sBasePath & """ /F /A > " & sTemp, xlHidden, True)

If Not lErrCode = 0 Then
    MsgBox "Problem " & "Error Code " & lErrCode
    Exit Sub
End If


Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile(sTemp, ForReading, False, TristateFalse)
strJson = TS.ReadAll
TS.Close

FSO.DeleteFile sTemp
Set FSO = Nothing
Set WSH = Nothing

Stop 'see what's in the string.
     'if it worked, then you can parse the results

End Sub
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • via Windows PowerShell and: curl -u 'username:token' 'https://api.dev.name.com/v4/domains:checkAvailability' -X POST --data '{"domainNames":"example.com"}' it says: {"message":"Invalid Argument","details":"Error occurred during parsing: Cannot decode json string."} – Opt Names Mar 22 '19 at 19:29
  • @OptNames As I wrote above, it will be hard to help you without `username:token`. Not sure why you are commenting here on your results using PowerShell, but if you do use PS, you need to call `curl.exe` since `curl` is an alias for `Invoke-WebRequest` – Ron Rosenfeld Mar 22 '19 at 19:58