Permalink
Browse files

Added ability to set headers in the JHttp options variable.

  • Loading branch information...
eddieajau committed Mar 27, 2012
1 parent b9cbb7e commit 7cc1c7fa6a97c86a633bd12d8447b4416d0e0d13
Showing with 97 additions and 3 deletions.
  1. +25 −2 docs/manual/en-US/chapters/classes/jhttp.xml
  2. +72 −1 libraries/joomla/http/http.php
@@ -18,7 +18,7 @@
the first transport that is supported (this will usually be the "curl" transport).</para>
<example>
- <title>Creating a JHttp object </title>
+ <title>Creating a JHttp object</title>
<programlisting>// Create an instance of a default JHttp object.
$http = new JHttp;
@@ -133,6 +133,29 @@ $response = $http-&gt;head('http://api.example.com/cars/1');</programlisting>
<section>
<title>Working with options</title>
- <para>Can you help improve this section of the manual?</para>
+ <para>Customs headers can be pased into each REST request, but they can also be set globally in the constructor options where
+ the registry path starts with "headers.". In the case where a request method passes additional headers, those will override
+ the headers set in the options.</para>
+
+ <example>
+ <title>Setting custom request headers</title>
+
+ <programlisting>// Create the options.
+$options = new JRegistry;
+
+// Configure a custom Accept header for all requests.
+$options-&gt;set('headers.Accept', 'application/vnd.github.html+json');
+
+// Make the request, knowing the custom Accept header will be used.
+$pull = $http-&gt;get('https://api.github.com/repos/joomla/joomla-platform/pulls/1');
+
+// Set up custom headers for a single request.
+$headers = array(
+ 'Accept' =&gt; 'application/foo',
+);
+
+// In this case, the Accept header in $headers will override the options header.
+$pull = $http-&gt;get('https://api.github.com/repos/joomla/joomla-platform/pulls/1', $headers);</programlisting>
+ </example>
</section>
</section>
@@ -35,7 +35,8 @@ class JHttp
/**
* Constructor.
*
- * @param JRegistry $options Client options object.
+ * @param JRegistry $options Client options object. If the registry contains any headers.* elements,
+ * these will be added to the request headers.
* @param JHttpTransport $transport The HTTP transport object.
*
* @since 11.3
@@ -89,6 +90,16 @@ public function setOption($key, $value)
*/
public function options($url, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('OPTIONS', new JUri($url), null, $headers);
}
@@ -104,6 +115,16 @@ public function options($url, array $headers = null)
*/
public function head($url, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('HEAD', new JUri($url), null, $headers);
}
@@ -119,6 +140,16 @@ public function head($url, array $headers = null)
*/
public function get($url, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('GET', new JUri($url), null, $headers);
}
@@ -135,6 +166,16 @@ public function get($url, array $headers = null)
*/
public function post($url, $data, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('POST', new JUri($url), $data, $headers);
}
@@ -151,6 +192,16 @@ public function post($url, $data, array $headers = null)
*/
public function put($url, $data, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('PUT', new JUri($url), $data, $headers);
}
@@ -166,6 +217,16 @@ public function put($url, $data, array $headers = null)
*/
public function delete($url, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('DELETE', new JUri($url), null, $headers);
}
@@ -181,6 +242,16 @@ public function delete($url, array $headers = null)
*/
public function trace($url, array $headers = null)
{
+ // Look for headers set in the options.
+ $temp = (array) $this->options->get('headers');
+ foreach ($temp as $key => $val)
+ {
+ if (!isset($headers[$key]))
+ {
+ $headers[$key] = $val;
+ }
+ }
+
return $this->transport->request('TRACE', new JUri($url), null, $headers);
}

0 comments on commit 7cc1c7f

Please sign in to comment.