I try to write a custom Restful API to send contents between two drupal site. The present code work except the datas are in a weird format with @ in the json.
My resquest is:
$serializer = \Drupal::service('serializer'); $data = $serializer->serialize($node, 'json', ['plugin_id' => 'entity']);
$response = \Drupal::httpClient()
->post("http://site1.test/api_test/send_content", [
'headers' => [
'Content-type' => 'application/json',
],
'json' => $data,
]);`
The function which process my request:
public function createNewContent(Request $request) {
if ( 0 === strpos( $request->headers->get( 'Content-Type' ), 'application/json' ) ) {
$data = json_decode( $request->getContent() );
\Drupal::logger('test_rest_api')->notice($data);
}
$response['data'] = 'Query well received';
$response['method'] = 'POST';
return new JsonResponse( $response );
}
The problem is, after the json_decode my datas let a big string and look like:
@"nid":[{"value":5197],"uuid":[@"value":"8310f782-06ce-4fb8-957d-ec7feecc8929"],"vid":[@"value":63527],"langcode":[@"value":"en"],"type":[@"target_id":"page","target_type":"node_type","target_uuid":"189f826c-e516-4a22-8d4d-f3a2f418aea9"],"revision_timestamp":[@"value":"2017-12-19T21:08:36+00:00","format":"Y-m-d\TH:i:sP"],"revision_uid":[@"target_id":1,"target_type":"user","target_uuid":"3e68a310-1da4-4691-8501-854c329d9899","url":"/fr/user/1"],"revision_log":[],"status":[@"value":true],"title":[@"value":"Test"],"uid":[@"target_id":1,"target_type":"user","target_uuid":"3e68a310-1da4-4691-8501-854c329d9899","url":"/fr/user/1"],"created":[@"value":"2017-12-19T21:07:57+00:00","format":"Y-m-d\TH:i:sP"],"changed":[@"value":"2017-12-19T21:08:36+00:00","format":"Y-m-d\TH:i:sP"],"promote":[@"value":true],"sticky":[@"value":false],"default_langcode":[@"value":true],"revision_translation_affected":[@"value":true],"moderation_state":[],"content_translation_source":[@"value":"und"],"content_translation_outdated":[@"value":false],"metatag":@"value":{"title":"Test","og_site_name":"Test","shortlink":"http://site1.test/en/node/5197/sendContent","canonical_url":"http://site1.test/en/node/5197/sendContent","og_type":"article","og_url":"http://site1.test/en/node/5197/sendContent","image_src":"http://site1.test/sites/default/files/styles/header_full/public/medias/img/hero_pr4_0282.jpg?itok=kSm8zY0q","og_title":"Test","og_description":"This is a test","og_image":"http://site1.test/sites/default/files/styles/header_full/public/medias/img/hero_pr4_0282.jpg?itok=kSm8zY0q","og_updated_time":"2017-12-19T16:08:36-05:00","article_published_time":"2017-12-19T16:07:57-05:00","article_modified_time":"2017-12-19T16:08:36-05:00"},"path":[@"alias":"/test","pid":16287,"langcode":"en"],"rh_action":[@"value":"bundle_default"],"rh_redirect":[],"rh_redirect_response":[@"value":301],"publish_on":[],"unpublish_on":[],"menu_link":[],"body":[@"value":"\u003Cp\u003EThis is a test\u003C/p\u003E","format":"full_html","summary":""]}
I precise the @ are already present before the json_decode. I don't know where I could have made a mistake.
I suppose I can clean it easily but I'm more interested to understand what is this format and how can I convert it properly and quickly.