PHP get_file_contents POST- no records detected

Hello,

i am trying to post data do a defined rest API.

$input = new \stdClass();
lastname = _POST[‘usrlastname’];
$input->familyname = $lastname;
firstname = _POST[‘usrfirstname’];
$input->firstname = $firstname;

$json = json_encode($input);
$data = ‘{“resource”’.’:’. ‘[’.$json.’]}’;

//Set stream options for api-access with post
$posts = array(‘http’ => array(
‘user_agent’ => ‘custom user agent string’,
‘method’ => ‘POST’,
‘ignore_errors’ => true,
‘header’ => “X-DreamFactory-Api-Key: $apikey”,
‘resource’ => $data));

$post = stream_context_create($posts);
$requesturl = $apiurl.’_table/Person?fields=UUID,LoginName’; // path to your JSON file
$request = file_get_contents($requesturl, false, $post); // put the contents of the file into a variable

When i try to post the data, i am getting the message, that:
REST Exception #400 > No record(s) detected in request. Please make sure record(s) are wrapped in a ‘resource’ tag. Example: {“resource”:[{“record”:1},{“record”:2}]}

But the records are included with
’resource’ => $data

and looks like this:
{“resource”:[{“familyname”:“Jacobi”,“firstname”:“Ren\u00e9”}]}

So the full request is formatted in this way:
Array (
[http] =>
Array (
[user_agent] => custom user agent string
[method] => POST
[ignore_errors] => 1
[header] => X-DreamFactory-Api-Key: key
[resource] => {“resource”:[{“familyname”:“test”,“firstname”:“user”}]} ) )

Resolved the issue by myself :smiley:

It was necessary to provide header information, that content type is application/json.

Her is the full working code to POST Information to dreamfactory the php file_get_contents:

$input = new \stdClass();
gender = _POST[‘gender’];
lastname = _POST[‘usrlastname’];
$input->familyname = $lastname;
firstname = _POST[‘usrfirstname’];
$input->firstname = $firstname;

$json = json_encode($input);
$data = ‘{“resource”’.’:’. ‘[’.$json.’]}’;

//Set stream options for api-access with post
$posts = array(‘http’ => array(
‘user_agent’ => ‘custom user agent string’,
‘method’ => ‘POST’,
‘ignore_errors’ => true,
‘header’ => array(‘Content-Type: application/json’."\r\n"
. ‘X-DreamFactory-Api-Key:’. $apikey."\r\n"
. 'Content-Length: ’ . strlen($data) . “\r\n”),
‘content’ => $data));

$post = stream_context_create($posts);
$requesturl = $apiurl.’_table/Person?fields=UUID,LoginName’; // path to your JSON file
$request = file_get_contents($requesturl, false, $post); // put the contents of the file into a variable