Skip to content

Commit e9635fa

Browse files
Fixing Content-Type for POST/PUT requests
1 parent 8f4037b commit e9635fa

File tree

13 files changed

+32
-34
lines changed

13 files changed

+32
-34
lines changed

lib/OpenCloud/CloudMonitoring/Resource/AbstractResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testParams($params = array(), $debug = false)
7878
// send the request
7979
$response = $this->getService()
8080
->getClient()
81-
->post($this->testUrl($debug), array(), $json)
81+
->post($this->testUrl($debug), self::getJsonHeader(), $json)
8282
->send();
8383

8484
return Formatter::decode($response);
@@ -97,7 +97,7 @@ public function test($debug = false)
9797
$this->checkJsonError();
9898

9999
$response = $this->getClient()
100-
->post($this->testExistingUrl($debug), array(), $json)
100+
->post($this->testExistingUrl($debug), self::getJsonHeader(), $json)
101101
->send();
102102

103103
return Formatter::decode($response);

lib/OpenCloud/CloudMonitoring/Resource/Alarm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function test($params = array(), $debug = false)
8585

8686
$response = $this->getService()
8787
->getClient()
88-
->post($url, array(), $body)
88+
->post($url, self::getJsonHeader(), $body)
8989
->send();
9090

9191
return Formatter::decode($response);

lib/OpenCloud/CloudMonitoring/Resource/Zone.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function traceroute(array $options)
5555
try {
5656
$response = $this->getService()
5757
->getClient()
58-
->post($this->url('traceroute'), array(), json_encode($params))
58+
->post($this->url('traceroute'), self::getJsonHeader(), json_encode($params))
5959
->send();
6060

6161
$body = Formatter::decode($response);

lib/OpenCloud/Common/Base.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use OpenCloud\Common\Exceptions\JsonError;
1515
use OpenCloud\Common\Exceptions\UrlError;
1616
use OpenCloud\Common\Collection\ResourceIterator;
17+
use OpenCloud\Common\Constants\Header as HeaderConst;
18+
use OpenCloud\Common\Constants\Mime as MimeConst;
1719

1820
/**
1921
* The root class for all other objects used or defined by this SDK.
@@ -423,4 +425,9 @@ public function stripNamespace($namespace)
423425
return end($array);
424426
}
425427

428+
protected static function getJsonHeader()
429+
{
430+
return array(HeaderConst::CONTENT_TYPE => MimeConst::JSON);
431+
}
432+
426433
}

lib/OpenCloud/Common/Http/Message/RequestSubscriber.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,10 @@ public static function getInstance()
2929
public static function getSubscribedEvents()
3030
{
3131
return array(
32-
'request.before_send' => 'ensureContentType',
3332
'curl.callback.progress' => 'doCurlProgress'
3433
);
3534
}
3635

37-
public function ensureContentType(Event $event)
38-
{
39-
if ($event['request'] instanceof EntityEnclosingRequest
40-
&& $event['request']->getBody()
41-
&& $event['request']->getBody()->getContentLength()
42-
&& !$event['request']->hasHeader(Header::CONTENT_TYPE)
43-
) {
44-
$event['request']->setHeader(Header::CONTENT_TYPE, 'application/json');
45-
}
46-
}
47-
4836
/**
4937
* @param $options
5038
* @return mixed

lib/OpenCloud/Common/PersistentObject.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
*/
7373
abstract class PersistentObject extends Base
7474
{
75-
7675
private $service;
7776
private $parent;
7877
protected $metadata;
@@ -182,7 +181,7 @@ public function create($params = array())
182181

183182
$createUrl = $this->createUrl();
184183

185-
$response = $this->getClient()->post($createUrl, array(), $json)->send();
184+
$response = $this->getClient()->post($createUrl, self::getJsonHeader(), $json)->send();
186185

187186
// We have to try to parse the response body first because it should have precedence over a Location refresh.
188187
// I'd like to reverse the order, but Nova instances return ephemeral properties on creation which are not
@@ -233,7 +232,7 @@ public function update($params = array())
233232
$this->checkJsonError();
234233

235234
// send the request
236-
return $this->getClient()->put($this->getUrl(), array(), $json)->send();
235+
return $this->getClient()->put($this->getUrl(), self::getJsonHeader(), $json)->send();
237236
}
238237

239238
/**
@@ -417,7 +416,7 @@ protected function action($object)
417416
$url = $this->url('action');
418417

419418
// POST the message
420-
return $this->getClient()->post($url, array(), $json)->send();
419+
return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
421420
}
422421

423422
/**

lib/OpenCloud/Compute/Resource/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function imageSchedule($retention = false)
327327
(object) array('retention' => $retention)
328328
);
329329
$body = json_encode($object);
330-
$request = $this->getClient()->post($url, array(), $body);
330+
$request = $this->getClient()->post($url, self::getJsonHeader(), $body);
331331
}
332332

333333
$body = Formatter::decode($request->send());

lib/OpenCloud/Compute/Resource/ServerMetadata.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function create()
108108
{
109109
return $this->getParent()
110110
->getClient()
111-
->put($this->url(), array(), $this->getMetadataJson())
111+
->put($this->getUrl(), self::getJsonHeader(), $this->getMetadataJson())
112112
->send();
113113
}
114114

@@ -123,7 +123,7 @@ public function update()
123123
{
124124
return $this->getParent()
125125
->getClient()
126-
->post($this->url(), array(), $this->getMetadataJson())
126+
->post($this->getUrl(), self::getJsonHeader(), $this->getMetadataJson())
127127
->send();
128128
}
129129

@@ -136,7 +136,7 @@ public function update()
136136
*/
137137
public function delete()
138138
{
139-
return $this->getParent()->getClient()->delete($this->url(), array());
139+
return $this->getParent()->getClient()->delete($this->getUrl(), array());
140140
}
141141

142142
public function __set($key, $value)
@@ -146,7 +146,7 @@ public function __set($key, $value)
146146
if ($this->key && $key != $this->key) {
147147
throw new Exceptions\MetadataKeyError(sprintf(
148148
Lang::translate('You cannot set extra values on [%s]'),
149-
$this->Url()
149+
$this->getUrl()
150150
));
151151
}
152152

lib/OpenCloud/Database/Resource/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function create($params = array())
106106
$this->checkJsonError();
107107

108108
// POST it off
109-
return $this->getClient()->post($url, array(), $json)->send();
109+
return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
110110
}
111111

112112
/**

lib/OpenCloud/ObjectStore/Resource/AbstractResource.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function getMetadata()
161161
*/
162162
public function saveMetadata(array $metadata)
163163
{
164-
$headers = self::stockHeaders($metadata);
164+
$headers = self::stockHeaders($metadata) + self::getJsonHeader();
165165
return $this->getClient()->post($this->getUrl(), $headers)->send();
166166
}
167167

@@ -190,9 +190,11 @@ public function unsetMetadataItem($key)
190190
{
191191
$header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX,
192192
static::METADATA_LABEL, $key);
193-
193+
194+
$headers = array($header => 'True');
195+
194196
return $this->getClient()
195-
->post($this->getUrl(), array($header => 'True'))
197+
->post($this->getUrl(), $headers)
196198
->send();
197199
}
198200

0 commit comments

Comments
 (0)