Help creating my first dataset using the API and VB


#1

I am trying to use the dataset API to create a dataset, but I am getting a 400 Bad Request Error. Could anyone who has implemented the API help me out by checking my request and code below and help me figure out what I am doing wrong?

Here is my json request:
{
“fields”: {
“clientName”: {
“type”: “string”,
“name”: “Client Name”,
“optional”: false},
“domainName”: {
“type”: “string”,
“name”: “Domain Name”,
“optional”: false},
“registrar”: {
“type”: “string”,
“name”: “Registrar”,
“optional”: false},
“domainStatus”: {
“type”: “string”,
“name”: “Domain Status”,
“optional”: false},
“creditHold”: {
“type”: “string”,
“name”: “Credit Hold”,
“optional”: false},
“expirationDate”: {
“type”: “date”,
“name”: “Expiration Date”}
}}

And here is the code I am using to try to create the dataset:

Public Sub CreateDataSet()
Dim postData As String = “”
Dim responseResults As String = “”
Try

        Dim sb As New StringBuilder
        sb.Append("{""fields"": ")
        sb.Append(" {")
        sb.Append("""clientName"": {")
        sb.Append("""type"": ""string"",")
        sb.Append("""name"": ""Client Name"",")
        sb.Append("""optional"": false}, ")

        sb.Append("""domainName"": {")
        sb.Append("""type"": ""string"",")
        sb.Append("""name"": ""Domain Name"",")
        sb.Append("""optional"": false}, ")

        sb.Append("""registrar"": {")
        sb.Append("""type"": ""string"",")
        sb.Append("""name"": ""Registrar"",")
        sb.Append("""optional"": false}, ")

        sb.Append("""domainStatus"": {")
        sb.Append("""type"": ""string"",")
        sb.Append("""name"": ""Domain Status"",")
        sb.Append("""optional"": false}, ")

        sb.Append("""creditHold"": {")
        sb.Append("""type"": ""string"",")
        sb.Append("""name"": ""Credit Hold"",")
        sb.Append("""optional"": false}, ")

        sb.Append("""expirationDate"": {")
        sb.Append("""type"": ""date"",")
        sb.Append("""name"": ""Expiration Date""}")


        sb.Append("},")
        sb.Append("""unique_by"": [""domainName""]")
        sb.Append("}")

        postData = sb.ToString

        Console.WriteLine(postData)

        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

        Dim byteArrayPostData As Byte() = Encoding.UTF8.GetBytes(postData)

        Dim request As WebRequest = WebRequest.Create("https://api.geckoboard.com/datasets/DomainsExpiringV1")
        request.Method = "PUT"

        'Authorization 
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes("myAPIKeyGoesHere:")
        request.Headers.Add("Authorization", "Basic " & System.Convert.ToBase64String(byteArray))
        request.PreAuthenticate = True

        'Content
        request.ContentType = "application/json"
        request.ContentLength = byteArrayPostData.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArrayPostData, 0, byteArrayPostData.Length)
        dataStream.Close()

        'Response
        Dim response As WebResponse = request.GetResponse()
        responseResults = (CType(response, HttpWebResponse).StatusDescription)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        responseResults = responseResults & Chr(10) & responseFromServer
        reader.Close()
        dataStream.Close()
        response.Close()

    Catch
        responseResults = Err.Description
    End Try

    Console.WriteLine(responseResults)

  
End Sub`Preformatted text`

#2

Hi @earnold :wave:

I noticed a couple things:

1 Perhaps this is just an issue with copy/pasting your code here, but the quotation marks on your code above don’t seem to pass JSON validation. The right JSON is:

{
	"fields": {
		"clientName": {
			"type": "string",
			"name": "Client Name",
			"optional": false
		},
		"domainName": {
			"type": "string",
			"name": "Domain Name",
			"optional": false
		},
		"registrar": {
			"type": "string",
			"name": "Registrar",
			"optional": false
		},
		"domainStatus": {
			"type": "string",
			"name": "Domain Status",
			"optional": false
		},
		"creditHold": {
			"type": "string",
			"name": "Credit Hold",
			"optional": false
		},
		"expirationDate": {
			"type": "date",
			"name": "Expiration Date"
		}
	}
}

2 The ID you are sending (DomainsExpiringV1) isn’t valid. Field IDs may only contain lowercase alphanumeric characters, dots, hyphens, and underscores domainsexpiringv1 or domains.expiring.v1 should work though


#3

Hi Luis,

I don’t see any differences in the JSON you posted vs the JSON I show at the top of my post (which is what my code builds as the request string, I am writing the postData string to the console and I copied it and pasted it from there.)

I did try changing the ID to domainsexpiringv1, but that did not fix the 400 error.


#4

Hi @earnold

Maybe I can clarify the issue.

The issue is the JSON you provided does not use standard quotation marks. JSON requires that you use a standard double quotation mark, or U+0022. What you are using is actually a left double quotation mark, or U+201C. This is common with Windows based systems as they often provide alternative ways to encode characters which do not meet the JSON format. You can see how your JSON is not considered valid if you paste your JSON into jsonlint.com, or you can see how your quotation marks are not the correct quotation marks if you paste one of them into the identification tool here: http://www.babelstone.co.uk/Unicode/whatisit.html

Does that make sense? Please let me know if you have any further questions about this issue.


#5

I cannot figure out how to paste it onto this page with the correct quotation marks, but when I write my json out to the console and copy and paste that into the json validator, it validates. When I paste it into this page, it changes the quotation marks.


#6

I have figured it out. I had capital letters in my field IDs and only lower case are allowed. Changing all the field IDs to lower case letters resolved the bad request errors.

Thanks for the help though!


#7

Glad to hear it’s sorted. I’ll make sure we mention that (avoid capital letters) for fields in the dev docs.