I am attempting to post the following JSON to my mysql database to add a new record.
{
"resource": {
"code": "466767"
},
"ids": [
0
],
"filter": "string",
"params": [
"string"
]
}
However the code i always get in response is:
No record(s) detected in request. Please make sure record(s) are wrapped in a ‘resource’ tag.
I have tried many things and I am out of ideas. The JSON works correctly when input into the live API docs.
Whole Code:
<?php
$verb = $event['request']['method'];
if ($verb !== 'GET') {
// currently always throws a 500 to client
throw new \Exception('Only HTTP GET is allowed on this endpoint');
}
$params = $event['request']['parameters'];
$required = ['jsonString', 'table'];
foreach ($required as $element) {
if (!isset($params[$element])) {
$event['response'] = [
'status_code' => 400,
'content' => [
'success' => false,
'message' => "Required parameter $element not found in request."
]
];
return;
}
}
$tableUsed = $params['table'];
$json_data = ($params['jsonString']);
$lang_data = json_decode($json_data);
$url = 'http://52.169.200.242/api/v2/mysql/_table/'.$tableUsed.'/?api_key=813b0d263f902fc8118336c76779a019a6ba541eeca062b08e877a9f27a941bb';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $lang_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
//execute post
$result = curl_exec($ch);
$info = curl_getinfo($ch);
//close connection
curl_close($ch);
return [
'result' => ($result)
];