Create dataset with .net webrequest


#1

Hi,

I have problems creating a simple dataset, getting a 400 response. Can anyone help me out.

Json sent as content as follows:

{
“fields”: {
“orderdate”: {
“type”: “date”,
“name”: “Orderdatum”,
“optional”: “false”},
“orders”: {
“type”: “number”,
“name”: “Olev Order”,
“optional”: “false”},
“delorders”: {
“type”: “number”,
“name”: “Lev Order”,
“optional”: “false”}
}
}

Private Function PUT_OrderDataSet() As String

    PUT_OrderDataSet = ""
    Try


        Dim postData As String = ""
        postData = postData & "{" & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """fields"": {" & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """orderdate"": {" & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """type"": ""date""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """name"": ""Orderdatum""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """optional"": ""false""}," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """orders"": {" & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """type"": ""number""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """name"": ""Olev Order""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """optional"": ""false""}," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """delorders"": {" & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """type"": ""number""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """name"": ""Lev Order""," & Chr(13) & Chr(10)
        postData = postData & Chr(9) & """optional"": ""false""}" & Chr(13) & Chr(10)
        postData = postData & "}" & Chr(13) & Chr(10)
        postData = postData & "}"

        Dim byteArrayPostData As Byte() = Encoding.UTF8.GetBytes(postData)
       
        Dim request As WebRequest = WebRequest.Create("https://api.geckoboard.com/datasets/garporderdata")
        request.Method = "PUT"

        'Authorization
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes("ourAPIkey:")
        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()
        PUT_OrderDataSet = (CType(response, HttpWebResponse).StatusDescription)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        PUT_OrderDataSet = PUT_OrderDataSet & Chr(10) & responseFromServer
        reader.Close()
        dataStream.Close()
        response.Close()


    Catch
        PUT_OrderDataSet = Err.Description
    End Try


End Function

#2

Hi @jono1071,

Sorry to hear that you’re having trouble!

It looks like the code you’re using to form the JSON for the dataset is pretty complex (and also in Visual Basic which is not a language I’m familiar with).

Would it be possible for you to share a log of the JSON that is returned from this and pushed to us?

Lisa


#3

{
“fields”: {
“orderdate”: {
“type”: “date”,
“name”: “Orderdatum”,
“optional”: “false”},
“orders”: {
“type”: “number”,
“name”: “Olev Order”,
“optional”: “false”},
“delorders”: {
“type”: “number”,
“name”: “Lev Order”,
“optional”: “false”}
}
}


#4

Hi again @jono1071,

Taking a closer look at the schema you’re using to create the dataset, we have picked up on a couple of things that could be causing issues.

The first is that you’ve used String values rather than Booleans when specifying that values are optional. i.e. "optional": "false" rather than "optional": false

Another issue is that only Number, Money and Percentage fields can be optional. It’s not possible for the Date field you’re using.

I was able to create a dataset with a few tweaks to your code. Here’s what I submitted:

{
  "fields": {
    "orderdate": {
      "type": "date",
      "name": "Orderdatum"
    },
    "orders": {
      "type": "number",
      "name": "Olev Order",
      "optional": false
    },
    "delorders": {
      "type": "number",
      "name": "Lev Order",
      "optional": false
    }
  }
}

If you’re still having problems with sending data to us once you’ve created the dataset in this way, please let me know and I’ll take another look. For this I would need the actual values that are being generated and sent.

Lisa


#5

Hi Lisa, with som help from your tweaks it worked, with no boolvalues as strings. I will try to fill the dataset with data now.

Thank you for your support

BR / Johan