ForumsDevelopersHow to POST for 'adding tasks'?


How to POST for 'adding tasks'?
Author Message
fahad

Posted: May 16, 2011
Score: 1 Reference
Hi

The documentation isn't clear what bit to 'POST' and what bit to pass as a URL parameter. By looking at the example of adding a task I see:

http://api.toodledo.com/2/tasks/add.php?key=YourKey;
tasks=[{"title"%3A"My Task"}%2C{"title"%3A"Another"%2C"star"%3A"1"%2C"ref"%3A"98765"}];
fields=folder,star

Does this mean that the URL to call would be:

http://api.toodledo.com/2/tasks/add.php?key=YourKey;

And then we POST the following bit to it?

tasks=[{"title"%3A"My Task"}%2C{"title"%3A"Another"%2C"star"%3A"1"%2C"ref"%3A"98765"}];
fields=folder,star


This looks like a 'mix' of POST variables and URL variables. If I try this I always get a 'No Title' error. If I however pass the 'key' bit as part of the POST string I simply got 'Invalid Key' or something.

Please kindly explain what part to post and what to pass as a paramater. If I send everything as part of the URL it works but that isn't what I want as there's too much text under 'notes' to pass.
Jake

Toodledo Founder
Posted: May 17, 2011
Score: 0 Reference
The examples show GET requests because they can be typed out easily. If you are using a POST, you would put all of the parameters (including key) into the POST body using the normal mechanism.

If you were using PHP, this is what you would do:

$params = "key=".$key."&tasks=".$encodedJSONString;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
fahad

Posted: May 17, 2011
Score: 1 Reference
Ah! Thanks, will give that a go.

I should mention the documentation still should then make clear that the paramters passed via POST need to use '&' and not ';'. We've been trying to POST this:

key=XYZ;tasks=...

which always returned a 'Invalid key' or 'key not found' error. The example too uses ';' as the separator between parameters but I guess for developers like me that take example word for word it should perhaps spell out the stuff the API is actually going to accept.

I think the code you just posted would serve way more than the actual URL that's on the page at the moment as it conveys a completely different thing (or is it really just me?).

Many thanks!
Jake

Toodledo Founder
Posted: May 17, 2011
Score: 0 Reference
Thanks. We'll work on the documentation.
dave

Posted: May 28, 2011
Score: 1 Reference
There needs to be better documentation for posting requests. I also cannot get posts to work at all. GET calls work just fine but post requests keep returning invalid key responses.

If I post a request to https://api.toodledo.com/2/tasks/edit.php with the POST payload shown below I get invalid key which does not make any sense based on what's discussed here.

I've replaced the ";" with "&" as the delimeter and that did not help. I'm also using the .net WebClient class which makes posting a request trivial. All combined post requests are still failing.

Here's the basic post code ...

var url = "http://api.toodledo.com/2/tasks/edit.php";
var bytes = Encoding.Default.GetBytes(postData);
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/json");
var response = client.UploadData(url, "POST", bytes);
Console.Write(Encoding.Default.GetString(response));
}

GET requests work perfectly. I can include the tassk, key, fields and response format. If I take the "payload" and switch it to a post nothing works.

Here's a sample request which I've tried both encoded and raw and I always get back "{"errorCode":1,"errorDesc":"Empty key"}". I also tried just posting "key=09c98d1b441ceb81bdae09191f9bb9d" and I still get back an invalid key message when the key is valid (swithcing to a get works just fine!)

key=09c98d1b441ceb81bdae09191f9bb9d&tasks=[{"id":"72985498","title":"test12345","context":"0","goal" :"0","location":"0","priority":"1","status":"0","star":"0","remind":"0","starttime":"0","duetime":"0 ","duedatemod":"0","repeat":"","tag":"","note":"Test1","parent":"0","meta":"","modified":"1306583509 ","order":"0","children":"0","repeatfrom":"0","length":"0","timer":"0","timeron":"0","added":"130658 4000"}]&fields=folder,context,goal,location,tag,startdate,duedate,duedatemod,starttime,duetime,remin d,repeat,status,star,priority,length,timer,added,note,parent,children,order,meta&f=xml
Jake

Toodledo Founder
Posted: May 31, 2011
Score: 1 Reference
I am not sure what is going wrong, but it must be some implementation detail of the POST because there are dozens of apps using the API 2.0 with POST, and the sample code I posted above works.

I can't see anything wrong with the code you posted. My only guess is that when you POST the data, it is encoding it again, so you may be double encoding the characters. I am not very familiar with .net, so I can't be certain, but I am fairly sure that the issue is not with the API.
dave

Posted: Jun 06, 2011
Score: -1 Reference
I still cannot get posts to work. I'm sure I'm going something dumb but I cannot figure out what it is.

Here is a sample RAW POST request and response pulled from fiddler ... I realize the tasks are missing but this is to try to isolate my issue to just getting the key to validate. I am sending the key so why is the error "Empty Key" being returned?



POST http://api.toodledo.com/2/tasks/add.php HTTP/1.1
Host: api.toodledo.com
Content-Length: 36

key=2f0ff8645ef10826b60658b3d23fb012



HTTP/1.1 200 OK
Date: Tue, 07 Jun 2011 03:44:44 GMT
Server: Apache/2.2.3 (Red Hat)
Content-Length: 39
Content-Type: text/html; charset=UTF-8

{"errorCode":1,"errorDesc":"Empty key"}
Jake

Toodledo Founder
Posted: Jun 07, 2011
Score: 1 Reference
I don't know what is happening, but it must be something to do with the POST implementation on your device. Here is what I would do. Create a simple html webpage with a form in it that is setup to POST its results to our API.

<form action="http://api.toodledo.com/2/tasks/add.php" method="post">
< input type="text" name="key" />
< input type="submit" />
</form>

Submit this form through a desktop web browser, which should work. I just tried it in Firefox. Now use your fiddler thing to see what it sent and compare to what you just showed me.


This message was edited Jun 07, 2011.
dave

Posted: Jun 15, 2011
Score: 1 Reference
The post request was in unicode instead of utf-8. Now everything works properly.
Jake

Toodledo Founder
Posted: Jun 15, 2011
Score: 1 Reference
Thanks for letting us know. I will update our documentation to mention that communication must be in UTF8
ch00k_1316112830

Posted: Sep 16, 2011
Score: -1 Reference
From what I can see, Toodledo API does not support sending a POST request with parameters in the request body, but only in the URL.
There is a Firefox add-on, REST Client (https://addons.mozilla.org/en-us/firefox/addon/restclient/). I tried to create a POST request with URL http://api.toodledo.com/2/notebooks/add.php and Request Body "key=2ce76378dcdf3dfa3d34c589920cece5" (without the quotes), and got {"errorCode":1,"errorDesc":"Empty key"}
However, sending a POST request with URL http://api.toodledo.com/2/notebooks/add.php?key=2ce76378dcdf3dfa3d34c589920cece5 and empty Request Body works and returns {"errorCode":4,"errorDesc":"You didn't send any notebooks to add."}
Can you confirm that Toodledo API does not support receiving parameters in request body?


This message was edited Sep 16, 2011.
ch00k_1316112830

Posted: Sep 16, 2011
Score: 1 Reference
I also tried this over telnet with the same result.

ayurchuk@ayurchuk:~$ telnet api.toodledo.com 80
Trying 72.4.112.215...
Connected to api.toodledo.com.
Escape character is '^]'.
POST /2/notebooks/add.php HTTP/1.1
Host: api.toodledo.com
Content-Length: 10

key=123qwe
HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 14:10:15 GMT
Server: Apache/2.2.3 (Red Hat)
Content-Length: 39
Content-Type: text/html; charset=UTF-8

{"errorCode":1,"errorDesc":"Empty key"}Connection closed by foreign host.
ayurchuk@ayurchuk:~$ telnet api.toodledo.com 80
Trying 72.4.112.215...
Connected to api.toodledo.com.
Escape character is '^]'.
POST /2/notebooks/add.php?key=123qwe HTTP/1.1
Host: api.toodledo.com
Content-Length: 0

HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 14:11:08 GMT
Server: Apache/2.2.3 (Red Hat)
Content-Length: 41
Content-Type: text/html; charset=UTF-8

{"errorCode":2,"errorDesc":"Invalid key"}

^C^]Connection closed by foreign host.
ayurchuk@ayurchuk:~$
Jake

Toodledo Founder
Posted: Sep 16, 2011
Score: 0 Reference
The Toodledo API does support POST with the parameters in the request body. There must be a syntax error in the way that you are doing it, or the plugin may be doing it wrong.
ch00k_1316112830

Posted: Sep 16, 2011
Score: 1 Reference
Can you try doing it yourself via telnet or REST Client add-on? Because I cannot see any errors in my requests. Maybe you could show how to post it correctly.

This message was edited Sep 16, 2011.
Jake

Toodledo Founder
Posted: Sep 16, 2011
Score: 0 Reference
Here is how I do it in PHP and I just tested it.


function postToUrl($request, $post_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

$response = curl_exec($ch);

if (curl_errno($ch)) {
trigger_error(curl_error($ch), E_USER_ERROR);
} else {
curl_close($ch);
}

return $response;
}


$params = "key=".$key;
$res = postToUrl($params, "http://api.toodledo.com/2/notebooks/add.php");


This message was edited Sep 16, 2011.
You cannot reply yet

U Back to topic home

R Post a reply

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