I want to send a patch request without a body, so in Postman, I only require to send the request without body, but in the frontend, I need the second argument if not I will get a 401 error.
I’ve tried an empty string, NULL, and empty object – but it’s still not working. It didn’t return the same output as in Postman:
export const somethingNeedsPatching = async (details) => {
const { token } = JSON.parse(localStorage.getItem("userData"));
const url = `${
import.meta.env.VITE_APP_SNATCH_URL
}/something/somethingPath/${details.something}
`;
try {
const res = await axios.patch(
url,
{},
{
headers: {
"Content-Type": "application/json",
"Content-Length": 0,
Authorization: `Bearer ${token}`,
},
}
);
return res;
} catch (error) {
console.log(error);
return error.response;
}
};
In Postman, the request is sent without a body and it works, but in the frontend, Axios requires a second parameter. If I left the url with the header, I’ll automatically get a 401 error.
2
Answers
I think may be due to the url also you may get the error so you may try as:
}/something/somethingPath/${details.something}
`;
and in header too you have to remove the content-length line and should try this as:
May be this will help you.
The main issue I see is that your URL has a trailing newline. Also, the URL in the screenshot has a trailing
/
.You also have redundant headers
content-type
andcontent-length
.In order to set no request body, you should be able to pass
null
orundefined
. Alternately, use the generalaxios()
method without setting adata
property.Separating
baseURL
andurl
can also make your code cleaner