c319113
c319113

Reputation: 215

POST requests require a Content-length header in laravel

       $output = Curl::httpGet("https://bhleh.com/emailstorage",  "POST", $params);                    
       return $output;

this is in my laravel but when ever i try to run this i get an error saying

411. That’s an error.

POST requests require a Content-length header. That’s all we know.

i tried adding header files in my middle ware folder but nothing seems to work. i figured out that the Content-length header isn't included in laravel (i think so not so sure) so how would i add it please note i am very new to laravel

Upvotes: 2

Views: 15274

Answers (2)

Bintu
Bintu

Reputation: 21

I was facing the same issue but I was not sending CURLOPT_POSTFIELDS as per the API endpoint. I don't need to send any variable but when I send CURLOPT_POSTFIELDS with a blank array it started working.

$data_string = array();
$ch = curl_init('https://bhleh.com/emailstorage');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
);

$result = curl_exec($ch);
return $result;

Upvotes: 2

c319113
c319113

Reputation: 215

this fix to this is that this i replaced the whole code with this

$data_string = json_encode($params);                                                                                                                                                                                                                   
    $ch = curl_init('https://bhleh.com/emailstorage');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);              
    return  $result;

Upvotes: 8

Related Questions