0

I wrote a program to post tasks to asana through the API and it has been working fine up until this morning, can anyone help me figure out why that is?

this is an example of the JSON string I am sending:

{"workspace":09876543321111,"data": {"assignee":null,"name":"Sample Name","notes":"Sample Noted","due_on":"2015-01-27","projects":"12434567889099","completed":false}}

and I am getting a 400 error: bad request.

this is my code:

string ID = "09876543321111"; //workspace ID
string url = @"https://app.asana.com/api/1.0/workspaces/" + ID + @"/tasks";
Data dat = new Data();

string ProjName = "Test Project"; 
dat.projects = "1234567890234";
dat.assignee = null; 
dat.name = "Sample Name";
dat.notes = "Sample Notes";
dat.due_on = val.requiredBy.Value.ToString("u").Substring(0, 10);
dat.completed = false;

//if task doesnt exist, make one
if (!Tasks.CheckExist(project, dat.projects, dat.name, apiKey, log))
{
    string json = JsonConvert.SerializeObject(dat);
    string data = "{\"workspace\":" + ID + ",\"data\": " + json + "}";
    log.WriteLine(data);
    Functions.Post(data, url, apiKey, log);                            
}

Post function:

//post tasks to asana
public static void Post(string data, string url, string apiKey, StreamWriter log)
{
    byte[] bytes = Encoding.UTF8.GetBytes(data);
    var req = (HttpWebRequest)WebRequest.Create(url);

    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();
    }
    catch (WebException ex)
    {
        HttpWebResponse response = ((HttpWebResponse)ex.Response);
        string exc = url + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription;
        Console.WriteLine(exc);
        log.WriteLine(exc);
    }
}

EDIT

for anyone who cares I solved the problem by changing string data to:

  string data = "{\"data\": " + json + "}";
Ira
  • 21
  • 6

1 Answers1

0

We recently made a change to return a 400 error if there were unexpected parameters passed at the top level, as (nearly) all API routes only use the parameters passed in under the "data" attribute. In this case (as you correctly determined) the "workspace" attribute at the top level was incorrect - previously we just ignored it, but in an effort to make the API less "surprising" we wanted to be explicit and strict about parameters that could be ignored, as otherwise it could be misleading.

agnoster
  • 3,744
  • 2
  • 21
  • 29