Hope you're doing well. I'm encountering an error in the aws amplify library called, aws-amplify/api
.
I have configured a post & get call, which work successfully. Additionally, for the get call, when I run it with a specific parameter - the customer Id, it returns only one result, and that result is the information of the customer with the matching customerId.
When I attempt to use the same endpoint as the 'get one' function I've set up uses for delete, it doesn't work. I'm getting a 404 error, which is confusing to me because the same endpoint exists for the 'get one' request, which functions successfully.
I've reviewed the AWS documentation on the del
call in the amplify api library. My code is pretty much the same as that code there, and for all intensive purposes, it is. I'm confused as to why the endpoint would be valid for a get call, but invalid for a delete call.
The profile I'm using on my computer has - as far as I know - all the privileges that a user profile can. Indeed, I used the most broad permission possible for my personal user in my AWS account.
I've tried reviewing a number of questions, but their all about GraphQL related to this topic - or their about being unable to delete an amplify resource - obviously not much help.
Below I've attached the relevant code for two of the functions. The function that works with the same endpoint, the 'get' function, and the 'delete' function - the one that doesn't work. However, the entire git repository is here if you find you need additional information in digging into this.
GetOneCustomer - Working
async function getOneCompany() {
try {
const restOperation = del({
apiName: GladwyneAPIName,
path: `${GladwynePath}/1`,
});
console.log("Get One Worked");
const { body } = await restOperation.response;
const response = await body.json();
console.log("Response Recieved: ", response);
return response;
} catch (error) {
console.log("Get Call Failed: ");
console.log(error.response.body);
}
}
Delete One Customer - Not Working
async function deleteOneCustomer(customer) {
try {
const returnedStatusCode = del({
apiName: GladwyneAPIName,
path: `/Gladwyne/Customer/${customer.CustomerId}`,
options: {
queryParams: {
CustomerId: customer.CustomerId,
},
},
});
const { body } = await returnedStatusCode.response;
const returnedResult = await body.json();
console.log("Returned Result: ", returnedResult);
return returnedResult;
} catch (error) {
if (error instanceof ApiError) {
if (error.response) {
const { statusCode, headers, body } = error.response;
console.error(
`Received ${statusCode} error response with payload: ${body}`
);
}
}
console.log("Plain old error message: ", error);
logger.error("Error from class: ", error);
}
}
I'm getting a response of, Unknown Error: Unknown Error
and this too
DELETE https://{ID}.execute-api.us-east-1.amazonaws.com/gladwyne/Gladwyne/Customer/3?CustomerId=3 404 (Not Found)
When I turn on the advanced logging, I am also returned this object. Here it is for what it is worth..
Received 404 error response with payload: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot DELETE /Gladwyne/Customer/3</pre>
</body>
</html>
Any thoughts/advice on this matter would be appreciated.
I ran into the same issue and found the solution by checking the generated Lambda code.
Instead of:
await del({
apiName: 'todo-api',
path: '/todo/1'
});
Do this:
await del({
apiName: 'todo-api',
path: '/todo/object/1'
});
The difference is the `object` word in the `path`. Honestly I don't exactly understand why the api is like this but it works.