So I have this web based application I’m trying to make and it needs to be able to communicate with an API I’ve been provided. I’ve made GET requests that work just fine, but when making PATCH requests, they keep returning status code 400 bad request and I can’t seem to find the error I’ve made. I’m using Node.js and fetch to make the requests.
In my example, in the patchData value there are more fields, but I decided to include just 4 for the example.
`app.patch('/patchRequest', async (req, res) => {
const length = Number(req.body.len);
const dist = Number(req.body.distance);
const width = Boolean(req.body.wide);
const height = Boolean(req.body.height);
const apiEndpoint = 'url that I've triple checked is correct';
const patchData = {'Field1': length, 'Field2': dist, 'Field3': width, 'Field4': height};
try {
const response = await fetch(apiEndpoint, {
method: 'PATCH',
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',`
'Cookie': 'Cookie used for autorization'
}
body: JSON.stringify(patchData)
});
if (response.ok) {
//handle the response
} else {
//handle failed query
}
} catch (error) {
console.error('Error:', error);
}
}`
laressoni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1