Jenkins start parameterized task with curl

How to start a parameterized task on a jenkins server with curl?

Start a task

The answer to this question seems simple at first after seen this page [1]. Unfortunately, it does not work as described:

vjp@inabox:~$ curl -i -X POST http://localhost:8080/job/inapp_GenerateDocs/build --form file0=@my-ina-project-src.tar.gz --form json='{"parameter": [{"name":"dl/sources.tar.gz", "file":"file0"}]}'
HTTP/1.1 100 Continue

HTTP/1.1 201 Created
Date: Fri, 21 Apr 2017 13:13:29 GMT
X-Content-Type-Options: nosniff
Location: http://localhost:8080/job/inapp_GenerateDocs/
Content-Length: 0
Server: Jetty(9.2.z-SNAPSHOT)

According to http://localhost:8080/job/inapp_GenerateDocs/api, the Location: header should contain the URL to the item in the queue, but it is not the case.

This issue was already pointed out by [2]. The documentation is wrong: build should be buildWithParameters:

vjp@inabox:~$ curl -i -X POST http://localhost:8080/job/inapp_GenerateDocs/buildWithParameters --form file0=@my-ina-project-src.tar.gz --form json='{"parameter": [{"name":"dl/sources.tar.gz", "file":"file0"}]}'
HTTP/1.1 100 Continue

HTTP/1.1 201 Created
Date: Fri, 21 Apr 2017 13:15:32 GMT
X-Content-Type-Options: nosniff
Location: http://localhost:8080/queue/item/19/
Content-Length: 0
Server: Jetty(9.2.z-SNAPSHOT)

Much better! Job #19 was started.

Check the progress of a task

The progress of the job can be checked with several output formats: xml, json or python. Example for json:

curl http://localhost:8080/queue/item/19/api/json?pretty=on

But there is a very weird behavior here. According to this discussion [3] and [4], this link only works while the task is in the queue and about 4-5 minutes after it left the queue. Later than this, and an Error 404 is given. Not a very friendly interface...