Welcome Guest, Not a member yet? Register   Sign In
How can get Response from API when status is 4xx using Curl library
#1

some API has set 400, 401 etc if have error in username or other param
in CI4 (v4.4.8) I cant get response for this.

any idea?
Reply
#2

Are you talking about CURLRequest?
See https://codeigniter.com/user_guide/libra...ttp-errors
Reply
#3

(06-06-2024, 08:55 PM)kenjis Wrote: Are you talking about CURLRequest?
See https://codeigniter.com/user_guide/libra...ttp-errors

yes, its about it.
but how to get response from API when its status code is 400, 401

I use try-catch, but in catch cant use like Guzzle

PHP Code:
try {
    $response $client->request('GET''http://example.com/api-endpoint');
    echo $response->getBody();
} catch (
RequestException $e) {
    // check response if fail cause some reason
    if ($e->hasResponse()) {
        $response $e->getResponse();
        echo 'HTTP Status Code: ' $response->getStatusCode() . "\n";
        echo 'Response Body: ' $response->getBody();
    } else {
        echo 'Request exception occurred without a response';
    }

Reply
#4

PHP Code:
$response $client->request('GET''/api-endpoint', ['http_errors' => false]);
dd($response); 

you may try add http_errors => false, like example above to get the response from API.
Reply
#5

(06-07-2024, 09:46 AM)SonzoXide Wrote:
PHP Code:
$response $client->request('GET''/api-endpoint', ['http_errors' => false]);
dd($response); 

you may try add http_errors => false, like example above to get the response from API.

many thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB