0

I'm trying to figure out how to post a new task to a user in asana, but I keep getting the 400 error code. Can anyone tell me what I am doing wrong.

This is what I have so far:

        string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string ID = "xxxxxxxxxxxxx";
        string url = @"https://app.asana.com/api/1.0/tasks";

        Data dat = new Data();
        dat.workspace = ID;
        dat.name = "Buy eggs";
        dat.notes = "Testing";
        string json = JsonConvert.SerializeObject(dat);
        string data ="\"data\": " + json;

        byte[] bytes = Encoding.UTF8.GetBytes(data);
        var req = (HttpWebRequest)WebRequest.Create(url);
        Console.WriteLine(bytes.ToString());
        req.Method = WebRequestMethods.Http.Post; 
        req.ContentLength = bytes.Length;
        req.ContentType = "application/json";

        var authInfo = apiKey + ":";
        var encodedAuthInfo = Convert.ToBase64String(
            Encoding.Default.GetBytes(authInfo));
        req.Headers.Add("Authorization", "Basic " + encodedAuthInfo);

        req.ContentLength = bytes.Length;
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();

        try
        {
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(res);
            Console.ReadLine();
        }
        catch (WebException ex)
        {
            HttpWebResponse response = ((HttpWebResponse)ex.Response);
            string e = url + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription;
            Console.WriteLine(e);
            Console.ReadLine();
        }

I put the serial converter, did i do it wrong?

Ira
  • 21
  • 6
  • 1
    your `data` is not a valid json string. Don't try to form it manually, use a real json parser like Json.Net. – EZI Jan 14 '15 at 19:56
  • Why not use newtonsoft to covert data as json than manual – Saravanan Jan 14 '15 at 19:58
  • Sorry im pretty new to programming, how would I go about doing that? – Ira Jan 14 '15 at 20:08
  • 1
    You can start by searching `Json.Net` and reading the docs... – EZI Jan 14 '15 at 20:12
  • I tried using the json converter, can anyone tell me what I did wrong :( – Ira Jan 14 '15 at 20:54
  • `var data = new { data = new { workspace = "", name = "", notes = "" } }; var json = JsonConvert.SerializeObject(data);` – EZI Jan 14 '15 at 20:59
  • @Ira HINT: the json in your first post has a root-object that contains a property `data` *(which has the properties like `workspace`)*. – EZI Jan 14 '15 at 21:11
  • ok im not getting an error anymore, but my task isn't showing up in Asana. This is the response im getting :{"data":{"id":xxxxxxxxxxx,"created_at":"2015-01-14T21:19:59.415Z","modified_at":"2015-01-14T21:19:59.415Z","name":"Buy eggs","notes":"Testing","completed":false,"assignee_status":"upcoming","completed_at":null,"due_on":null,"workspace":{"id":xxxxxxxxxxx,"name":"xxxxxxxxxxxx"},"num_hearts":0,"assignee":null,"parent":null,"hearts":[],"followers":[{"id":xxxxxxxxxx,"name":"xxxxxxxxxxx"}],"projects":[],"tags":[],"hearted":false}} – Ira Jan 14 '15 at 21:23

2 Answers2

1

As the first comment points out, you need to serialize the data properly. The final body posted should look something like:

{ "data": { "name": "My Example Task", ... }}

(Except of course with the ... replaced with more fields.)

agnoster
  • 3,744
  • 2
  • 21
  • 29
1

I have researched a long and found below code useful and working. I can successfully post Task to Asana through my application. I have used Json serializer for dot net. Thanks to Newtonsoft.Json.

        string json = null;
        byte[] bytes = null;
        string url = "https://app.asana.com/api/1.0/tasks";
        HttpWebRequest req = default(HttpWebRequest);
        Stream reqStream = default(Stream);
        string authInfo = null;
        Task TaskData = new Task();

        try
        {
            authInfo = apiKey + Convert.ToString(":");

            TaskData.workspace = WorkspaceId;
            TaskData.name = TaskName;
            TaskData.notes = TaskNotes;

            json = JsonConvert.SerializeObject(TaskData);
            json = json.Insert((json.Length - 1), ",\"projects\":[" + ProjectId + "]");
            json = Convert.ToString("{ \"data\":") + json + "}";

            bytes = Encoding.UTF8.GetBytes(json);

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = WebRequestMethods.Http.Post;
            req.ContentType = "application/json";
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
            req.ContentLength = bytes.Length;

            reqStream = req.GetRequestStream();
            reqStream.Write(bytes, 0, bytes.Length);
            reqStream.Close();


            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(res);
            Console.ReadLine();

            string finalString = res.Remove(0, 8);
            finalString = finalString.Remove((finalString.Length - 1));
            AsanaObjectId newtask = JsonConvert.DeserializeObject<AsanaObjectId>(finalString);

            return newtask;

        }
        catch (WebException ex)
        {
            HttpWebResponse response = (HttpWebResponse)ex.Response;
            string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
            MessageBox.Show(resp);
            System.Environment.Exit(0);
        }