Skip to content

Commit ce318e1

Browse files
committed
Only set Content-Type for POST requests
1 parent cc50f69 commit ce318e1

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/Resolver/Service.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,13 @@ public function resolve($definition)
4949
throw new \InvalidArgumentException('$definition must be an instance of ' . Definition::class);
5050
}
5151

52-
$headers = ['Content-type' => 'application/json'];
53-
5452
$url = $this->getIterator()->get('url', $definition);
5553
$query = $this->getIterator()->get('query', $definition);
5654
$method = $definition->has('method') ? $this->getIterator()->get('method', $definition) : 'POST';
5755
$variables = $definition->has('variables') ? $this->getIterator()->get('variables', $definition) : [];
5856
$ignoreSSLErrors = $definition->has('ignoreSSLErrors')
5957
? $this->getIterator()->get('ignoreSSLErrors', $definition)
6058
: false;
61-
$headers = $definition->has('headers')
62-
? array_merge($headers, $this->getIterator()->get('headers', $definition))
63-
: $headers;
6459
$requestParams = [
6560
'query' => $query,
6661
'variables' => $variables,
@@ -73,14 +68,23 @@ public function resolve($definition)
7368
CURLOPT_SSL_VERIFYPEER => !$ignoreSSLErrors,
7469
],
7570
]);
71+
7672
$client->setMethod($method);
77-
$client->setHeaders($headers);
73+
7874
if ($method === 'POST') {
75+
$headers = $definition->has('headers')
76+
? array_merge(['Content-type' => 'application/json'], $this->getIterator()->get('headers', $definition))
77+
: ['Content-type' => 'application/json'];
78+
7979
$client->setRawBody(json_encode($requestParams));
8080
} elseif ($method === 'GET') {
81+
$headers = $definition->has('headers') ? $this->getIterator()->get('headers', $definition) : [];
82+
8183
$client->setParameterGet($requestParams);
8284
}
8385

86+
$client->setHeaders($headers);
87+
8488
$response = $client->send();
8589

8690
return json_decode($response->getBody(), true);

test/Resolver/ServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function testResolveWithConfiguration(): void
133133
$zendClientMock = Mockery::mock('overload:' . Client::class);
134134
$zendClientMock->shouldReceive('setMethod')->with('GET');
135135
$zendClientMock->shouldReceive('setHeaders')
136-
->with(['Content-type' => 'application/json', 'header' => 'headerValue']);
136+
->with(['header' => 'headerValue']);
137137
$zendClientMock->shouldReceive('setParameterGet')->with($expectedRequestBody);
138138
$zendClientMock->shouldReceive('send')->andReturn($responseMock);
139139

0 commit comments

Comments
 (0)