0

I am getting an error:

400 Bad request

when trying to create a project via vb.net in asana.

Note: The ApiKey I am using works when I use it in other vb.net code to get the list of workspaces which is where I got my workspace ID.

Here is my code; I would be grateful for any insight...

Public Sub main()

    Dim address As Uri
    address = New Uri("https://app.asana.com/api/1.0/projects")

    Dim ApiKey As String
    ApiKey = "<my api key>"
    Dim basicAuthenticationString As String
    basicAuthenticationString = Convert.ToBase64String(New UTF8Encoding().GetBytes(ApiKey + ":"))

    ' Create the web request
    Dim request As HttpWebRequest
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)
    request.Headers("Authorization") = "Basic " & basicAuthenticationString
    request.Method = "POST"
    request.ContentType = "application/json"

    Dim postData As String = "{""data"":[{""name"":""Randy Test Project"",""notes"":""Randy Test Project Notes"",""workspace"":5272875888767}]}"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentLength = byteArray.Length

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim response As HttpWebResponse = request.GetResponse()
    Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
    dataStream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Console.WriteLine(responseFromServer)
    reader.Close()
    dataStream.Close()
    response.Close()

    Exit Sub

End Sub
Morix Dev
  • 2,700
  • 1
  • 28
  • 49
Randy G
  • 19
  • 4

2 Answers2

1

I was able to figure it out, My address needed to be:

Dim address As Uri = New Uri("app.asana.com/api/1.0/teams/22956925957833/projects")

Then my postData needed to be:

Dim postData As String = "{""data"":{""name"":""Randy Test Project"",""notes"":""Randy Test Project Notes""}}"

Randy G
  • 19
  • 4
0

Alternatively, you can specify the team or workspace in the post data. When you get a 400 Bad Request the body of the error response will tell you which fields were actually missing/invalid.

agnoster
  • 3,744
  • 2
  • 21
  • 29