php - Delete call curl returns 400
Get the solution ↓↓↓I am debugging why am I getting 400 response every time from the Laravel server.
When I am calling using Postman, everything works fine, but when I call from my curl script it returns 400 everytime.
My curl code looks like this:
$endpoint = "http://sup.l/api/iasku/IA00000001-My Beat-29999-H?";
$additional_headers = "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zdXAubG9jYWxcL2FwaVwvbG9naW4iLCJpYXQiOjE2MDE2MjI2MDQsImV4cCI6MTYwMTcwOTAwNCwibmJmIjoxNjAxNjIyNjA0LCJqdGkiOiJITFAyUEhWeEdQU1J0NWFQIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.3t15l573A_EHotUq6Ud3fcGegXZh1tGsMf3i9BlrVWU";
$headers = array_merge([
'Content-Type: application/json',
'Accept: application/json',
], $additional_headers );
$options = [
CURLOPT_URL => $endpoint,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_RETURNTRANSFER => 1, // return web page
CURLOPT_HEADER => 0, // don't return headers
CURLOPT_FOLLOWLOCATION => 1, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "myuser.agent", // who am i
CURLOPT_AUTOREFERER => 1, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => 0, // Disabled SSL Cert checks,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HTTPHEADER => $headers,
];
if (count($body)) {
$options['CURLOPT_POSTFIELDS'] = json_encode($body);
}
$ch = curl_init();
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
My curl command looks like this (works fine):
curl --request DELETE \
--url http://sup.l/api/iasku/IA00000001-My%20Beat-29999-H? \
--header 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zdXAubG9jYWxcL2FwaVwvbG9naW4iLCJpYXQiOjE2MDE2MjI2MDQsImV4cCI6MTYwMTcwOTAwNCwibmJmIjoxNjAxNjIyNjA0LCJqdGkiOiJITFAyUEhWeEdQU1J0NWFQIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.3t15l573A_EHotUq6Ud3fcGegXZh1tGsMf3i9BlrVWU'
Can anyone see what is the problem here? And how should I debug this?
Answer
Solution:
The problem here was that my URL had spaces, so the server couldn't process it.
Once I addedrawurlencode
for this part of URLIA00000001-My Beat-29999-H?
it started working as expected.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.