autopilotdev / autopilotdev.github.io

Autopilot Developer Platform documentation portal and API documentation.
2 stars 0 forks source link

The remote server returned an error: (400) Bad Request. Request was not formatted correctly #14

Closed tooms closed 8 years ago

tooms commented 8 years ago

I'm using the sample Visual Basic code for adding/updating a contact in Autopilot.

My function is as follows (API Key Removed).

The message returned is:

"The remote server returned an error: (400) Bad Request. {"error":"Bad Request","message":"Request was not formatted correctly. Please see http://developers.autopilothq.com for documentation."}"

and I'm a bit stuck on where to go from here.

As an aside, the same VB code provided is incorrect, and I have had to make adjustments as below to get this far.

I can manually enter the JSON produced by this function into the Console on the documentation website and the method works, that Json sting is:

{ "contact": { "FirstName": "Michael", "LastName": "Field", "Email": "michael-field@live.co.uk","custom": {"string--Conv_call_notes": "TEST"}}}

`    Public Function AutopilotUpdateContact(ByVal pFirstName As String, ByVal pLastName As String, ByVal pEmail As String, ByVal pNotes As String) As String

    Dim request = TryCast(System.Net.WebRequest.Create("https://api2.autopilothq.com/v1/contact"), System.Net.HttpWebRequest)

    request.Method = "POST"

    request.Headers.Add("autopilotapikey", "")
    request.ContentType = "application/json"

     Dim jsonString As String = "{ ""contact"": { ""FirstName"": """ + pFirstName + """, ""LastName"": """ + pLastName + """, ""Email"": """ + pEmail + """,""custom"": {""string--Conv_call_notes"": """ + pNotes + """}}}"

    Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(jsonString)

    'request.ContentLength = byteArray.Length

    Using writer = New System.IO.StreamWriter(request.GetRequestStream())
        'Dim writer = New System.IO.StreamWriter(request.GetRequestStream())
        writer.Write(byteArray)
        'Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", request.ContentLength)
        writer.Close()
    End Using
    Dim responseContent As String

    Try

        Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
            Using reader = New System.IO.StreamReader(response.GetResponseStream())
                responseContent = reader.ReadToEnd()
            End Using
        End Using

    Catch e As WebException

        responseContent = e.Message.ToString + " " + e.Status.ToString

    Finally

    End Try

    Return responseContent
    'Return jsonString

End Function`
tooms commented 8 years ago

I have solved the issue.

System.IO.StreamWriter does not write bytes, and therefor cannot be used.

Instead I used a BinaryWriter

        Using writer As BinaryWriter = New BinaryWriter(request.GetRequestStream())
            writer.Write(byteArray)
            writer.Close()
        End Using

I am disappointed there has been no response to this, and disappointed the provided code is not correct.

chrissharkey commented 8 years ago

Hi Tooms,

Nice work solving the issue.

The provided code is automatically generated by Apiary and since we don't understand all of those programming languages we aren't able to manually debug them all. We provide them as a convenience for our clients but obviously having your program work is your responsibility. Our API works as it is documented.

Chris Sharkey.