Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid header error on sending a request with curl

Tags:

php

curl

here is my code

echo 22;
$url = 'http://www.10bet.com/pagemethods.aspx/GetLeaguesContent';

$fields = array(    
        'BranchID' => urlencode('1') , 
        'LeaguesCollection' => urlencode('10098') , 
                );

$fields_string  = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');



$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json; charset=utf-8'));
$result = curl_exec($ch);
curl_close($ch);
var_dump( $result );

i keep getting

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.
like image 226
max Avatar asked Feb 15 '23 11:02

max


2 Answers

You are surprisingly getting the error

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.

Because you use an invalid header name :)

You use

Content-Type : application/json; charset=utf-8

Notice the space in the header name

The correct usage would be:

Content-Type: application/json; charset=utf-8
like image 104
ZombieSpy Avatar answered Feb 17 '23 11:02

ZombieSpy


In case anyone hit this as described by @ZombieSpy, and wonder where this strictness comes about, this is as defined in https://datatracker.ietf.org/doc/html/rfc7230#section-3.2, as follows:

3.2.  Header Fields

   Each header field consists of a case-insensitive field name followed
   by a colon (":"), optional leading whitespace, the field value, and
   optional trailing whitespace.
like image 35
sta nce io Avatar answered Feb 17 '23 09:02

sta nce io