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.

6
  • Have you tried Range("C1").Value? I know nothing about curl, but that Range value jumps out wrong to me Commented Mar 22, 2019 at 17:29
  • Have you reviewed the other similar questions on SO? Commented Mar 22, 2019 at 17:50
  • Yes, several internet resources reviewed. Commented Mar 22, 2019 at 17:55
  • If those did not help, without real credentials to access that site, it'll be difficult to help you. Commented Mar 22, 2019 at 17:58
  • You don't have anything in your send line where POST body should go Commented Mar 22, 2019 at 18:34

3 Answers 3

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

Did you try this please?
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

1 Comment

Unfortunately, same.
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

2 Comments

via Windows PowerShell and: curl -u 'username:token' '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."}
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.