0

Having a problem with guzzle and json in a symfony console app, my code looks like this:

$client = new Client([
    'timeout' => 15,0,
]);

$body =[
    "revision" => "1",
    "changelog" => "stuff",
    "description" => "Testing",
    "user" => "Foo bar",
];
var_dump(json_encode($body));

$request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", array(), ['deployment' => json_encode($body)]);

$response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);

The response I get is:

 Invalid resource type: array
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MiSc
  • 53
  • 6
  • HAve a look to your logs and paste the full error code. – Weenesta - Mathieu Dormeval Dec 13 '16 at 09:11
  • There is nothing in the log regarding this. – MiSc Dec 13 '16 at 09:38
  • It seems it should be `$request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", ['deployment' => json_encode($body)]);` ? – goto Dec 13 '16 at 09:39
  • No, that array is needed there (it is for header). – MiSc Dec 13 '16 at 10:15
  • For curl it is this: ` curl -X POST 'https://api.newrelic.com/v2/applications/someappid/deployments.json' \ -H 'X-Api-Key:somkey' -i \ -H 'Content-Type: application/json' \ -d \ '{ "deployment": { "revision": "1", "changelog": "stuff", "description": "Testing", "user": "Mikke" } }' ` – MiSc Dec 13 '16 at 10:19
  • Possible duplicate of : http://stackoverflow.com/questions/38633068/guzzle6-error-invalid-resource-type-array-when-send-a-guzzlehttp-psr7-request – Weenesta - Mathieu Dormeval Dec 13 '16 at 12:32

2 Answers2

0

According to this (Guzzle6), you must specify content-type, try :

$client = new Client([
    'timeout' => 15,0,
]);

$body = [ 'deployment' => [ 
             "revision" => "1", 
             "changelog" => "stuff", 
             "description" => "Testing", 
             "user" => "Foo bar", 
           ] 
        ];

$request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", ["content-type"=>'application/json'], json_encode($body));

$response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);

Hope this helps !

Community
  • 1
  • 1
  • What is the version of Guzzle you use ? – Weenesta - Mathieu Dormeval Dec 13 '16 at 12:45
  • Try to send json_encode($body) as the last parameter – Weenesta - Mathieu Dormeval Dec 13 '16 at 12:45
  • Tha last parameter is json_encode($body) - or am I missundersatnding you? Changed code a bit, but same problem. ``$body = [ 'deployment' => [ "revision" => "1", "changelog" => "stuff", "description" => "Testing", "user" => "Foo bar", ] ]; $client = new Client([ 'timeout' => 15,0, ]); $request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", array(), ['json' => $body]); $response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);\ – MiSc Dec 13 '16 at 13:18
0

Got it sorted:

$body = ['deployment' => [
    "revision" => "1",
    "changelog" => "stuff",
    "description" => "Testing",
    "user" => "Foo bar",
  ]];


$client = new Client([
  'timeout' => 15,0,
]);

$request = new GuzzleRequest(
  'POST',
  "https://api.newrelic.com/v2/applications/$appId/deployments.json",
  ["content-type" => 'application/json'],
  json_encode($body)
);
$response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);
MiSc
  • 53
  • 6