$headers = array( 'x-api-key' => '12345'
$http = HttpFactory::getHttp();
$http->post('url', 'data', $headers);
CURLOPT_HTTPHEADER set as:
x-api-key: 12345
CURLOPT_HTTPHEADER set as:
1: x-api-key: 12345
the additional 1: is added here:
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.
Labels |
Added:
J3 Issue
|
Status | New | ⇒ | Discussion |
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)
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.
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]',
);
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-04-10 14:34:15 |
Closed_By | ⇒ | Ruud68 |
I'm not following your issue here. If you're doing a
var_dump($headerArray)
then you should be seeing something similar to this: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 theHttp::post()
method and standardizes the header into a string notation (and in newer code also supports nested arrays). Essentially, you are getting this change: