Hi all,
I am a bit confused on how to handle this
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxx'
])->post('https://xxxxxx.com', [
'id' => "add-on",
'test' => $result,
]);
My problem comes with the response. The key fails I get something like this:
{
"error": {
"message": "Incorrect API key provided: xxxxxxxxxxxxxxxxxxxxxxxx'. You can find your API key at https://xxxxx.com.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
But when the key is fine, I get this:
{"id":"COMP-XXXXXXXXXXXXXX","object":"text_OK","created":1657056253,"backup":"123456789"}
As you can see the third party API is not following a similar output on success and fail. How would you then parse this and return an error on a bad key or a success when everything is fine. I tried this (below) but it fails when the key is right, error being "Undefined array key "error" ":
if($response && $response['error']['code'] === "invalid_api_key"){
return response()->json(['error' => 'There is a problem with our website, please try again later'], 500);
}
return response()->json(['success' => 'success!'], 200);