ForumsDevelopersAdd task v3 with C# Asp.Net


Add task v3 with C# Asp.Net
Author Message
sclassing

Posted: Dec 16, 2021
Score: -1 Reference
Hello,

I am trying to add a task with C# Asp.Net using HttpClient with version 3 of the API. I have successfully authenticated and gotten an access token. I'm able to use that access_token to get a list of tasks successfully. However, when I try to add a task, it's not working. I get a success response, but I don't see the task added.

Can someone please help understand what I'm doing wrong? Alternately if you could point me to a sample project for C# that might be helpful as well.

Thanks,
Steve

Here are the pertinent lines from my code:

---
/* includes */
using System.Net.Http;
using System.Net.Http.Headers;

---

/* subset of sample code */

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.toodledo.com/3/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var newTask = new ToodledoTask() { title = "Test Task", priority = 3 };
var url = string.Format("tasks/add.php?access_token={0}", _accessToken);
var responseTask = client.PostAsJsonAsync(url, newTask);
HttpResponseMessage response = responseTask.GetAwaiter().GetResult();

var responseSuccess = response.IsSuccessStatusCode; // <-- returns true, but fails to add task
}

---


public class ToodledoTask
{
public string title { get; set; }
public int priority { get; set; }
}
sclassing

Posted: Dec 21, 2021
Score: 1 Reference
I finally was able to successfully add a new task from C#. In case this might help someone else, here is the sample code:

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.toodledo.com/3/");

var url = string.Format("tasks/add.php?access_token={0}", _accessToken);

var toodleTask = "[{\"title\":\"NewTask\",\"priority\":3}]";

var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("tasks", toodleTask)
});

var responseTask = client.PostAsync(url, formContent);
var response = responseTask.GetAwaiter().GetResult();

var responseSuccess = response.IsSuccessStatusCode; // <-- returns true, and adds task correctly
}
You cannot reply yet

U Back to topic home

R Post a reply

To participate in these forums, you must be signed in.