3
votes

I am using laravel framework. I am trying to update an existing data using api (kinda new to this).

So this is my route

Route::group(['middleware' => 'web'], function () {
  Route::post('/update_supplier_details/{id}', "UpdateController@update_supplier_details");
});

This is my Controller

public function update_supplier_details(Request $request, $id){
    $details = $request->all();
    $client = new CharmeAPI;
    $token = Session::get('token');
    $url = "https://api.charmeapp.com/api/v1/suppliers/{$id}?token={$token}";
    $response = $client->request('POST', $url,['form_params' => $details])->getBody();
    echo $response;
    $data = json_decode($response, true);
    $status =  array_get($data, 'status');
    $message = array_get($data, 'error.msg');
    if($status == 'error'){
        session(['update_supplier_details_error' => $status]);
        return redirect()->back()->with('supplier_details_msg', $message);
    }
    else if($status == 'ok') {
        session(['update_supplier_details_error' => $status]);
        session(['supplier_details_first_name' => array_get($data, 'data.Supplier.first_name')]);
        session(['supplier_details_last_name' => array_get($data, 'data.Supplier.last_name')]);

        $first_name = session('supplier_details_first_name');
        $last_name = session('supplier_details_last_name');
        return $first_name.$last_name;
        return redirect()->back()->with('supplier_details_msg', $first_name.' '.$last_name.' added successfully');
    }
  }
}

and I am getting this error -

ServerException in RequestException.php line 107: Server error: POST https://api.charmeapp.com/api/v1/suppliers/139?token=Q8vJLPvpnRImoz5Li4tVfGtGliyGBQcx3NdqYbNdRaYYvsaoLncyDvFHkriS resulted in a 500 Internal Server Error response:

<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex,nofollow" />
<style>

(truncated...)

But if I use postman to call the api url I get the desired data Any help pls

1
You need to find out what the server error is. Enable debugging for your application (use APP_DEBUG=true on your .env file, make sure to change it back to false after your are done debugging, since your are using a live application for that). On your browser, use developer tools to see the error message. Then post it here for help if you can't find a solution.crabbly
@crabbly I did just that and I had this error message localhost/charme_admin/public/update_customer_details/154 Failed to load resource: the server responded with a status of 500 (Internal Server Error)Aderibigbe Temilola
ugh i need the answer to this too. did you ever figure it out?caro
@cora Yeah I did. Make sure you pass the correct parameters and token to the api.Aderibigbe Temilola

1 Answers

0
votes

Make sure you pass the correct parameters and token to the api.