Support array for json_data posted in rest/json service (#99)

Previous syntax :
```
CURLOPT_POSTFIELDS => array(
	'auth_user' => 'admin',
	'auth_pwd' => 'admin',
	'json_data' => '{
	   "operation": "core/get",
	   "class": "Person",
	   "key": "SELECT Person", "limit": "10", "page": "1"
	}'
);
```

Now we can also use :
```
CURLOPT_POSTFIELDS => array(
	'auth_user' => 'admin',
	'auth_pwd' => 'admin',
	"json_data[operation]" => "core/get",
	"json_data[class]" => "Person",
	"json_data[key]" => "SELECT Person",
	"json_data[limit]" => 10,
	"json_data[page]" => 1
);
```
This commit is contained in:
TAHRI Ahmed R
2020-03-27 18:11:09 +01:00
committed by GitHub
parent ff2e1a3507
commit 79cfb95f6e

View File

@@ -79,7 +79,7 @@ try
utils::UseParamFile();
$oKPI->ComputeAndReport('Data model loaded');
$iRet = LoginWebPage::DoLogin(false, false, LoginWebPage::EXIT_RETURN); // Starting with iTop 2.2.0 portal users are no longer allowed to access the REST/JSON API
$oKPI->ComputeAndReport('User login');
@@ -130,11 +130,26 @@ try
{
throw new Exception("Missing parameter 'json_data'", RestResult::MISSING_JSON);
}
$aJsonData = @json_decode($sJsonString);
if ($aJsonData == null)
if (is_string($sJsonString))
{
throw new Exception("Parameter json_data is not a valid JSON structure", RestResult::INVALID_JSON);
}
$aJsonData = @json_decode($sJsonString);
}
elseif(is_array($sJsonString))
{
$aJsonData = (object) $sJsonString;
$sJsonString = json_encode($aJsonData);
}
else
{
$aJsonData = null;
}
if ($aJsonData == null)
{
throw new Exception('Parameter json_data is not a valid JSON structure', RestResult::INVALID_JSON);
}
$oKPI->ComputeAndReport('Parameters validated');