Jump to content

How to debug when curl POST fails with client-side error 400?


Recommended Posts

Hi all,

I have a curl question. I'm working with an API provider that has Oauth2 authentication, no problem there: I can authenticate and receive the token correctly from within a module.  However, sending an authenticated POST request seems to fail with a client-side error (400) and I can't see where the issue is.

I've successfully run the same POST request from command line curl (using the same payload built with the same php code and output to a json file), and it works. I'm running presta inside a container, so I thought that was the issue but it does not seem to be it, because I can run the same curl request from inside the container's command line curl and it also works.

I noticed that in presta it immediately fails and in the command line, it takes some time until the request is delivered. So, I've increased the timeouts on the php curl options, but I'm running out of ideas.

When curl is in verbose mode, here's the output of the part related to the server-side:

* We are completely uploaded and fine                
* old SSL session ID is stale, removing              
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!                                               
* The requested URL returned error: 400              
* stopped the pause stream!  

 

Here's my curl options:

$header =  array(                                                                                                                                                                              
'Accept: application/json',                                                                                                                                                                              
'Content-Type: application/json',                                                                                                                   
'Authorization: Bearer mytoken'
);

$payload = json_encode(array("this"=>"that", ...));

$curl_options = array(                                                                                                                                                                                       
                CURLOPT_RETURNTRANSFER => true, // return content                                                                                                                                                    
                CURLOPT_HEADER => false,        // dont return headers                                                                                                                                               
                CURLOPT_FOLLOWLOCATION => true, // follow redirects                                                                                                                                                  
                CURLOPT_USERAGENT => 'mymodule',// whoami                                                                                                                                                            
                CURLOPT_ENCODING => '',         // handle all encodingst                                                                                                                                             
                CURLOPT_FAILONERROR => true,    // fail on error                                                                                                                                                     
                CURLOPT_AUTOREFERER => true,    // set referer on redirect                                                                                                                                           
                CURLOPT_VERBOSE => true,        // extra log to stderr                                                                                                                                               
                CURLOPT_TIMEOUT => 120,         // timeout on response                                                                                                                                               
                CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect                                                                                                                                                
                CURLOPT_MAXREDIRS => 10,        // stop after 10 redirects                                                                                                                                           
                CURLOPT_SSL_VERIFYPEER => false, // disable ssl cert checks                                                                                                                                          
                CURLOPT_HTTPHEADER => $header,                                                                                                                                                                       
                CURLOPT_POST => true,                                                                                                                                                                                
                CURLOPT_POSTFIELDS => $payload,                                                                                                                                                                      
        );                                           

$response = curl_exec($ch);                                                                                                                                                                                  

After running the above, $response is false, and the error code is 400.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...