J3 Issue ?
avatar Ruud68
Ruud68
10 Apr 2019

Steps to reproduce the issue

$headers = array( 'x-api-key' => '12345'
$http = HttpFactory::getHttp();
$http->post('url', 'data', $headers);

Expected result

CURLOPT_HTTPHEADER set as:
x-api-key: 12345

Actual result

CURLOPT_HTTPHEADER set as:
1: x-api-key: 12345

System information (as much as possible)

the additional 1: is added here:

$headerArray[] = $key . ': ' . $value;

Additional comments

I am currently investigation as to why API calls I do to an external provider don't work. the added key number turned out to be the issue.
I have tried to find information as to what is the correct value for the CURLOPT_HTTPHEADER, all I could find was that they should be set with the array values and NOT with the $key: $value (added key#:).

But this must be here for some reason... Can somebody enlighten me :)

If the way Joomla! does it can be improved upon, Im glad to do the PR for it.

avatar Ruud68 Ruud68 - open - 10 Apr 2019
avatar franz-wohlkoenig franz-wohlkoenig - change - 10 Apr 2019
Labels Added: J3 Issue
avatar franz-wohlkoenig franz-wohlkoenig - labeled - 10 Apr 2019
avatar franz-wohlkoenig franz-wohlkoenig - change - 10 Apr 2019
Status New Discussion
avatar mbabker
mbabker - comment - 10 Apr 2019

I'm not following your issue here. If you're doing a var_dump($headerArray) then you should be seeing something similar to this:

array(1) {
  [0] =>
  string(16) "x-api-key: 12345"
}

This is fully expected. The keys in this array aren't used by curl (or any of the other transports).

The line you highlighted takes the $headers associative array you injected into the Http::post() method and standardizes the header into a string notation (and in newer code also supports nested arrays). Essentially, you are getting this change:

// Passing this...
$headers = array(
    'x-api-key' => '12345',
);

// ... becomes this
$headers = array(
    'x-api-key: 12345',
);
avatar Ruud68
Ruud68 - comment - 10 Apr 2019

Hi,
when I do a var_dump($headers) (what I pass in the ->post) and a var_dump($headersArray) (what is set in the CURLOPT_HTTPHEADER) in Curltransport I get:

/var/www/html/project/equities/libraries/src/Http/Transport/CurlTransport.php:126:
array (size=6)
  0 => string 'Date: Wed, 10 Apr 2019 13:53:38 GMT' (length=35)
  1 => string 'Authorization: Auth [secret]' (length=65)
  2 => string 'Content-Md5: [secret]==' (length=37)
  3 => string 'Content-Type: application/json' (length=30)
  4 => string 'X-Mitter-Application-Access-Key: [secret]' (length=49)
  5 => string 'Nonce: [secret]' (length=57)

/var/www/html/project/equities/libraries/src/Http/Transport/CurlTransport.php:131:
array (size=6)
  0 => string '0: Date: Wed, 10 Apr 2019 13:53:38 GMT' (length=38)
  1 => string '1: Authorization: Auth [secret]' (length=68)
  2 => string '2: Content-Md5: [secret]==' (length=40)
  3 => string '3: Content-Type: application/json' (length=33)
  4 => string '4: X-Mitter-Application-Access-Key: [secret]' (length=52)
  5 => string '5: Nonce: [secret]' (length=60)
avatar Ruud68
Ruud68 - comment - 10 Apr 2019

Also found this: https://tools.ietf.org/html/rfc2616#section-4.2

Each header field consists of a name followed by a colon (":") and the field value.

What happens here is that the header field consists of a number followed by a colon followed by the name folowed by a colon forllowed by the field value.

avatar mbabker
mbabker - comment - 10 Apr 2019

Your issue is you need to pass an associative array, not an already formatted array.

It looks like you're using this for your $headers array:

$headers = array(
    'Date: Wed, 10 Apr 2019 13:53:38 GMT',
    'Authorization: Auth [secret]',
    'Content-Md5: [secret]==',
    'Content-Type: application/json',
    'X-Mitter-Application-Access-Key: [secret]',
    'Nonce: [secret]',
);

This is incorrect. It needs to be an associative array where the key is the header name and the value is the header value.

$headers = array(
    'Date' => 'Wed, 10 Apr 2019 13:53:38 GMT',
    'Authorization' => 'Auth [secret]',
    'Content-Md5' => '[secret]==',
    'Content-Type' => 'application/json',
    'X-Mitter-Application-Access-Key' => '[secret]',
    'Nonce' => '[secret]',
);
avatar Ruud68
Ruud68 - comment - 10 Apr 2019

owww @mbabker , you just made my day :) that is indeed the what happened, shame on me for copying from the API providers documentation :(

avatar Ruud68 Ruud68 - change - 10 Apr 2019
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2019-04-10 14:34:15
Closed_By Ruud68
avatar Ruud68 Ruud68 - close - 10 Apr 2019

Add a Comment

Login with GitHub to post a comment