https://discord.gg/botpress logo
#🤝help
AxiosError
# 🤝help
n

nutritious-guitar-71142

01/14/2024, 6:40 AM
I want to call API in execute code i am getting this error, i tried alot of other things still getting fetch error. this is the error massage in the log: Error: {"message":"Request failed with status code 400","name":"AxiosError","stack":"AxiosError: Request failed with status code 400\n at jNa (https://studio.botpress.cloud/assets/vendor-5be2f0b.js:389:969)\n at XMLHttpRequest.d (https://studio.botpress.cloud/assets/vendor-5be2f0b.js:389:4132)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json"},"withCredentials":false,"baseURL":"","method":"post","url":"***","data":"{\"headers\":{\"Content-Type\":\"application/json\",\"campaignname\":\"test3\",\"date\":\"test3\",\"hashtags\":\"test3\"}}"},"code":"ERR_BAD_REQUEST","status":400} this is the code: const apiUrl = '***' axios.post(apiUrl, { headers: { 'Content-Type': 'application/json', 'campaignname': 'test3', 'date': 'test3', 'hashtags': 'test3' } }) .then((response) => { console.log(response.data) }) .catch((error) => { console.error('Error:', error) })
q

quick-musician-29561

01/14/2024, 7:08 AM
Hi, without testing it, based on the code and error message now it seems that you have all the request payload inside headers, like this
Copy code
js
const apiUrl = ''

axios.post(apiUrl, {
  headers: { 'Content-Type': 'application/json', 'campaignname': 'test3', 'date': 'test3', 'hashtags': 'test3' }
})
  .then((response) => {
    console.log(response.data)
  })
  .catch((error) => {
    console.error('Error:', error)
  })
try something like this
Copy code
js
const apiUrl = '';

axios.post(apiUrl, {
  campaignname: 'test3',
  date: 'test3',
  hashtags: 'test3'
}, {
  headers: {
    'Content-Type': 'application/json'
  }
})
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error('Error:', error);
});
check the correct syntax from the APIs official docs
n

nutritious-guitar-71142

01/14/2024, 9:33 AM
@quick-musician-29561 it didnt work, the service i am requesting is working when i request it from VS code or postman however it is not working when i am requesting it from online compiler
2 Views