0
votes

i'm trying to send some data to my api with PHP OAuth Library (https://www.phpclasses.org/package/7700-PHP-Authorize-and-access-APIs-using-OAuth.html) but every time an error occurs

Error: it was not possible to access the API call: it was returned an unexpected response status 400 Response: {"message":"Error converting value \"{\"lastName\":\"Smith\",\"firstName\":\"John\",\"webAddresses\":{\"value\":\"[email protected]\"},\"phoneNumbers\":{\"value\":\"0666864\"}}\" to type...

This is my code:

$params = array(
  'x-api-key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Content-Type' => 'application/json',
  'Accept' => 'application/json'
);
$fields =  array(
  "lastName" => $entries[$tmp_id]["2.6"],
  "firstName" => $entries[$tmp_id]["2.3"],
  "webAddresses" => array([
    "value" => $entries[$tmp_id]["4"]
  ]),
  "phoneNumbers" => array([
    "value" => $entries[$tmp_id]["5"]
  ])
);
$json = json_encode($fields);

$success = $client->CallAPI(
  'https://myurl.com/v2/api',
  'POST', $json, array('FailOnAccessError'=>true, 'RequestHeaders' => $params,'RequestContentType' => 'application/json', 'FollowRedirection' => true, ), $user);

Any ideas what am i doing wrong? Regards

1
“Error converting value [...] to type...” - to what type? You might have left out the most important part of the error message here. - CBroe
@CBroe - to type "Company_Name.Docs.PublicApi.ViewModels.Write.Persons.Create.PersonModel". - troxxx

1 Answers

0
votes

I found the solution :)

i removed json encoding before send request and 'Content Type' from headers.

Working code:

$params = array(
  'x-api-key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'Accept' => 'application/json'
);
$fields =  array(
  "lastName" => $entries[$tmp_id]["2.6"],
  "firstName" => $entries[$tmp_id]["2.3"],
  "webAddresses" => array([
    "value" => $entries[$tmp_id]["4"]
  ]),
  "phoneNumbers" => array([
    "value" => $entries[$tmp_id]["5"]
  ])
);

$success = $client->CallAPI(
  'https://myurl.com/v2/api',
  'POST', $fields, array('FailOnAccessError'=>true, 'RequestHeaders' => $params,'RequestContentType' => 'application/json', 'FollowRedirection' => true, ), $user);