Kimai2: POST API Timesheet: unknown extra field in php-curl. Example please!

Created on 16 Sep 2019  路  7Comments  路  Source: kevinpapst/kimai2

V1.2

I'm trying to make an API POST from php-curl to
{{baseUrl}}/api/timesheets
and in
vendor/symfony/form/Extension/Validator/Type/FormTypeValidatorExtension.php
the
'allow_extra_fields' => false
is causing my curl deep trouble. As soon as I put it to "true" it works.
I don't know what the heck curl is adding to the request.
If I hijack the json string and paste it into Postman, it works.

Would you please make a PHP example? Thanks.

PS off-topic:
Creating a tag via API on-the-fly works, but I cannot delete the Timesheet afterwards via GUI if that tag is present. I need to edit-delete-delete it.

support

Most helpful comment

If it works for you in postman you can create the php code yourself in postman. Click Code -> select php -> select curl

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://demo.kimai.org/api/timesheets",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\r\n  \"begin\": \"2019-09-17T21:09:06\",\r\n  \"end\": \"2019-09-17T21:09:06\",\r\n  \"project\": 1,\r\n  \"activity\": 1,\r\n  \"description\": \"string\",\r\n  \"fixedRate\": 0,\r\n  \"hourlyRate\": 0\r\n}",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 173",
    "Content-Type: application/json",
    "Cookie: PHPSESSID=62sthvu9pcbj13dlp3o43ptccc",
    "Host: demo.kimai.org",
    "Postman-Token: 3695b181-ab82-402a-806e-93a0c266f918,f1911ca7-d82f-43b2-84a5-bf923544dd31",
    "User-Agent: PostmanRuntime/7.17.1",
    "X-AUTH-TOKEN: api_kitten",
    "X-AUTH-USER: susan_super",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}



All 7 comments

If it works for you in postman you can create the php code yourself in postman. Click Code -> select php -> select curl

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://demo.kimai.org/api/timesheets",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\r\n  \"begin\": \"2019-09-17T21:09:06\",\r\n  \"end\": \"2019-09-17T21:09:06\",\r\n  \"project\": 1,\r\n  \"activity\": 1,\r\n  \"description\": \"string\",\r\n  \"fixedRate\": 0,\r\n  \"hourlyRate\": 0\r\n}",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 173",
    "Content-Type: application/json",
    "Cookie: PHPSESSID=62sthvu9pcbj13dlp3o43ptccc",
    "Host: demo.kimai.org",
    "Postman-Token: 3695b181-ab82-402a-806e-93a0c266f918,f1911ca7-d82f-43b2-84a5-bf923544dd31",
    "User-Agent: PostmanRuntime/7.17.1",
    "X-AUTH-TOKEN: api_kitten",
    "X-AUTH-USER: susan_super",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}



LOL, Postman creates weird code. But hey, it's an PHP example. Thanks @hmr-it-jr !

There is already a JS demo in the documentation:
https://www.kimai.org/documentation/rest-api.html#demo

Regarding the PS_
I cannot imagine why that should happen, but if you can verify the mentioned bug in the demo installation, please open a bug issue.

Does the Postman code work for you?
It doesn't work for me.
{"code":400,"message":"Validation Failed","errors":{"errors":["This form should not contain extra fields."],"children":{"begin":{},"end":{},"project":{},"activity":{},"description":{},"tags":{}}}}
Would you guide me as to where I could intercept that call in symfony? A suitable function where that happens? I don't know Symfony yet...
Thanks
M

Remove all non-required fields.
The fields "fixed rate" and "hourly rate" cannot be set by every user.
Which data can be submitted depends on your system permissions and obviously the user you are using for the request.

Whoah, BEAUTIFUL! Thanks, now I get it.
I was using hourlyRate with a dummy user.
So symfony's answer basically means: "children:XYZ only, nothing else please"?

Yep. The "children" in the error messages give you the allowed (known) fields.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. If you use Kimai on a daily basis, please consider donating to support further development of Kimai.

Was this page helpful?
0 / 5 - 0 ratings