Search code examples
phpcurlguzzlewunderlist

Wunderlist’s API with Guzzle - Create Task - Bad Request 400


I'm trying out the Wunderlist API and have been following this helpful tutorial. They also provide a demo on Github. It works fine with the GET calls (and PATCH), but when I try to create a new task with a POST request, it returns bad request, client error: 400.

WunderlistClient.php

public function createTask($name, $list_id, $task = []) {
    if (!is_numeric($list_id)) {
        throw new \InvalidArgumentException('The list id must be numeric.');
    }
    $task['name'] = $name;
    $task['list_id'] = $list_id;
    $response = $this->client->post('tasks', ['body' => json_encode($task)]);
    $this->checkResponseStatusCode($response, 201);
    return json_decode($response->getBody());
}

index.php

try {
    $created = $wunderlist->createTask('New Task', $list_id);
    dump($created);
}
catch(\Exception $exception) {
    dump($exception);
}

I added a createList function, which only requires title, and that did work.

public function createList($title, $list = []) {
    $list['title'] = $title;
    $response = $this->client->post('lists', ['body' => json_encode($list)]);
    $this->checkResponseStatusCode($response, 201);
    return json_decode($response->getBody());
}

Why am I unable to create tasks?


Solution

  • I just had to switch 'name' to 'title'.

    public function createTask($title, $list_id, $task = []) {
        if (!is_numeric($list_id)) {
            throw new \InvalidArgumentException('The list id must be numeric.');
        }
        $task['title'] = $title;
        $task['list_id'] = $list_id;
        $response = $this->client->post('tasks', ['body' => json_encode($task)]);
        $this->checkResponseStatusCode($response, 201);
        return json_decode($response->getBody());
    }