JSON Object returned with error from LARAVEL API not readable in Nodejs
I am using Laravel apis for my project. The API is working fine when I use curl to access it.
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz", "filterZip":"123123"}' http://localhost:8080/api/signup {"error":{"message":"User with specified email address already started sign up process, but did not finished it. Please sign in regulary to continue sign up process.","errorCode":"020102"}}
However when I call if from nodejs app. I am unable to access the error Json Object from the response.
From Nodejs I am calling my laravel API
fetch(API_URI + '/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
On the Laravel API end following code is used to return response.
public function respondError($errorId, $statusCode, $message = 'Unknown') {
$this->setHttpStatusCode($statusCode);
$errorCode = ErrorHelper::generateErrorCode($this->controllerErrorId, $this->methodErrorId, $errorId);
if (API_LOG_ERROR_CODES) { ErrorHelper::logErrorCode($errorCode, $message); }
return $this->respond([
'error' => [
'message' => $message,
'errorCode' => $errorCode
]
]);
}
public function respond($dataForJson, $headers = []) {
return response()->json($dataForJson, $this->getHttpStatusCode(), $headers);
}
from Laravel Questions and Answers https://laravelquestions.com/php/json-object-returned-with-error-from-laravel-api-not-readable-in-nodejs/
via Lzo Media
No comments:
Post a Comment