Sep 14, 2020
0
Level 2
Rappresentation form-params in Body Request Third Api
Hi all, they gave me some third party api as in the next example:
/endpoints/{id} method 'PATCH'
Request Body:
{
"id": 0,
"Environment": {
"timestampAST": "2020-09-14T07:23:52.825Z",
"content": "string"
},
"lastCommEPT": "2020-09-14T07:23:52.825Z",
"lastCommAST": "2020-09-14T07:23:52.825Z",
"firstSeenAST": "2020-09-14T07:23:52.825Z",
"status": "ONLINE",
"isCritical": true,
"endpointType": "MPA",
"queueName": "string",
"hostname": "string",
"installationSite": {
"id": 0,
"name": "string",
"customerId": 0
},
"firstHash": "string"
}
in laravel i have this:
Class Endpoint
namespace App\Util;
use GuzzleHttp\Client;
class Endpoint
{
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function patchEndpointsId($id)
{
return $this->endpointPatchRequest('/endpoints/'.$id, $id);
}
public function endpointPatchRequest($url, $id)
{
try {
$request = $this->client->request('PATCH',$url, [
'body' => [
"id" => $id,
"Environment"=> [
"timestampAST" => "2020-09-11T15:11:54.508Z",
"content" => "string"
],
"lastCommEPT"=> "2020-09-11T15:11:54.508Z",
"lastCommAST"=> "2020-09-11T15:11:54.508Z",
"firstSeenAST"=> "2020-09-11T15:11:54.508Z",
"status"=> "ONLINE",
"isCritical"=> true,
"endpointType"=> "MPA",
"queueName"=> "string",
"hostname"=> "string",
"installationSite"=> [
"id"=> 0,
"name"=> "string",
"customerId"=> 0
],
"firstHash"=> "string"
],
'headers' => ['Content-Type' => 'application/json']
]);
} catch (\Exception $e) {
return [];
}
return $this->response_handler($request->getBody()->getContents());
}
public function response_handler($response)
{
if ($response) {
return json_decode($response);
}
return [];
}
}
EndpointsController:
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Util\Endpoint;
use Illuminate\Http\Request;
class EndpointsController extends Controller
{
protected $endpoints;
public function __construct(Endpoint $endpoints)
{
$this->endpoints = $endpoints;
}
public function patchEndpointsId($id)
{
$endpoint = $this->endpoints->patchEndpointsId($id);
return json_encode($endpoint);
}
is correct the syntax of how I pass the body request into the API?
thank you very much for help already
Please or to participate in this conversation.