Geckoboard Developer Community

Updating datasets using .Net


I’m working on a integration of datasets, but keep getting a bad request (400) response. According to the posts available online the data looks right, but no luck here. I’m also interested in how to define data sets using the same structure. Any help is much appreciated!

JSON sent

{“Data”: [ {“date”: “2016-01-01”,“quantity”: 819},{“date”: “2016-01-0”,“quantity”: 409},{“date”: “2016-01-03”,“quantity”: 164}]}

Please see the code snippet below.

Public Class JsonResultModel
    Public Results As String
    Public ErrorMessage As String
    Public IsSuccess As Boolean
End Class

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim con As New SqlConnection()
        Dim sb As New StringBuilder
        sb.Append("{""Data"": [")
        sb.Append(" {")
        sb.Append("""date"": ""2016-01-01"",")
        sb.Append("""quantity"": 819")
        sb.Append("""date"": ""2016-01-0"",")
        sb.Append("""quantity"": 409")
        sb.Append("""date"": ""2016-01-03"",")
        sb.Append("""quantity"": 164")
        Dim Result As JsonResultModel = HTTP_PUT("", sb.ToString())

    End Sub

    Public Function HTTP_PUT(Url As String, Data As String) As JsonResultModel

        Dim model As New JsonResultModel()
        Dim Out As String = String.Empty
        Dim strError As String = String.Empty
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(Url)
        Dim authInfo As String = "aaaaaaaa"
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
        req.Headers("Authorization") = "Basic " + authInfo
        req.PreAuthenticate = True

            'req.UseDefaultCredentials = False

            req.Method = "PUT"
            req.Timeout = 100000
            req.ContentType = "application/json"
            Dim sentData() As Byte = Encoding.UTF8.GetBytes(Data)
            req.ContentLength = sentData.Length

            Using sendStream As System.IO.Stream = req.GetRequestStream()

                sendStream.Write(sentData, 0, sentData.Length)

            End Using

            Dim res As System.Net.WebResponse = req.GetResponse()
            Dim ReceiveStream As System.IO.Stream = res.GetResponseStream()
            using sr As System.IO.StreamReader = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8)

                Dim read(265) As Char
                Dim count As Integer = sr.Read(read, 0, 256)

                While count > 0
                    Dim str As String = New String(read, 0, count)
                    Out += str
                    count = sr.Read(read, 0, 256)
                End While

            End Using 

        Catch ex As ArgumentException

            strError = String.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message)
        catch  ex As WebException

            strError = String.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message)

        catch  ex as Exception
            strError = String.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message)
        End Try

        model.Results = Out
        model.ErrorMessage = strError
        If Not String.IsNullOrWhiteSpace(Out) Then
            model.IsSuccess = True
        End If
        Return model
    End Function


Hi Christian, thanks for asking about this!

A couple of issues I see why you’d be getting back a 400 request:

In your code you’re sending your API key in the request body,

Dim sb As New StringBuilder 
sb.Append("{""Data"": [") 

which is how the custom widgets worked, though with the newer Datasets platform, the API key is sent as an HTTP Basic Auth header – the key is the ‘username’ and the password is blank.

I do see that you have this in your code,

Dim authInfo As String = "aaaaaaaa" 
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)) 
req.Headers("Authorization") = "Basic " + authInfo 

But you’ll want to replace “aaaaaaaa” with your API key and a colon, i.e. "XXX:".

Let me know how that works for you and if you have any other questions!