I deployed a PHP API that receives a file, a text parameter and an authorization header.
When I curl my API from my terminal works fine:
curl -v --request POST --header "Content-Type:multipart/form-data" \
--form image=@examples/sample.jpeg \
--form projectID="xxxx" \
-H "key:mykey" https://api.example.com/v1/api.php/image
Now, using vanilla JS and a HTML form as this, works fine as well:
formElem.onsubmit = async (e) => {
e.preventDefault();
let body = new FormData(formElem);
body.append("projectID", "xxxx");
let response = await fetch('https://api.example.com/v1/api.php/image', {
method: 'POST',
body: body,
headers: {
"key": "mykey"
}
});
let result = await response.json();
I have also tried from other languages and works fine.
However, when I send the request using NodeJS I get this error message:
406 Not Acceptable! An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security
I understand 406 errors are related to these headers:
- Accept
- Accept-Encoding
- Accept-Language
I've tried everything I could think of with those headers, even forcing them to be exactly as the ones sent with the code that works but I still get the 406 error.
How else can I debug this? It's crazy that it works with other languages but not with NodeJS. Is NodeJS adding something extra to the requests that triggers this error?
This is the code that runs the fetch using Axios:
async function getURLImage(image,projectID) {
const requestBody = new FormData()
requestBody.append('image', fs.createReadStream(image))
requestBody.append('projectID', projectID)
let response = await axios.post(baseURL,requestBody, {
headers: {
...requestBody.getHeaders(),
"key": key
},
})
.then(data => {
console.log(requestBody)
console.log('then:', data.data)
console.log('then:', data)
})
.catch(err => {
console.log(requestBody)
console.log('catch',err.data)
console.log('catch',err)
})
}
Same thing without using Axios:
async function getURLImage(image,projectID) {
const requestBody = new FormData()
requestBody.append('image', fs.createReadStream(image))
requestBody.append('projectID', projectID)
let response = await fetch(baseURL,{
method: 'POST',
body: requestBody,
headers: {
"key": key
},
})
.then(data => {
console.log(requestBody)
console.log('then:', data.headers)
console.log('then:', data)
return data.json()
})
.then(data => {
console.log('then2:', data)
})
.catch(err => {
console.log(requestBody)
console.log('catch',err.data)
console.log('catch',err)
})
}
Any ideas would be extremely helpful. Thanks!