400 Bad Request trying to do any GETs


#1

Hi

I am new to using API’s and having an issue with using the GET.

I am writing my code in VB.net and have got the code working for approving the app and exchanging the auth code for the access token. But when I try to do any GETs such a list invoices or even just display company info, I get a 400 code.

This is my code, a valid access token is being passing into the sub.

I have tried removing the accept and contenttype headers, but to no avail.

Public Sub get_invoices(access_token As String)
    Dim responsedata As String

    Dim hwrequest As Net.HttpWebRequest = Net.HttpWebRequest.Create("https://api.sandbox.freeagent.com/v2/company")

    hwrequest.Accept = "application/json"
    hwrequest.ContentType = "application/json"
    hwrequest.Headers.Add("Authorization", "Bearer " & access_token)
    hwrequest.ContentLength = 0 '

    hwrequest.Method = "GET"

    Try

        Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
        If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader = New IO.StreamReader(hwresponse.GetResponseStream())
            responsedata = responseStream.ReadToEnd()
        End If

    Catch ex As Exception
        Console.WriteLine(ex.Message)

    End Try


End Sub

On executing the line:

Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()

I am getting the 400 error.

I have done the same request with google playground and it is working fine.

Any ideas?

Regards

Daren


#2

Hi, Daren.

I would imagine this is an issue with VB.net and the way it adds headers (which unfortunately I have no experience in). The values you’re adding are all correct so I’m wondering if the keys are wrong. If VB.net is able to output the request itself, that would be helpful.

In curl it would look something like:

> curl https://api.sandbox.freeagent.com/v2/company -H "Authorization: Bearer TOKEN_HERE" -H "Accept: application/json" -H "Content-Type: application/json"

< {"company":{<snipped>}}


#3

Thanks George. I’m not sure if VB.net can output the request. Seems weird that VB.net would add the headers in a incorrect way. Any ideas any other way of doing it?