API from Botpress to local host is not working
# 🤝help
f
I'm having trouble with API calls to my localhost in Botpress. When I test the API using Postman, everything works fine, but when I try to make the same call within Botpress, I'm getting a response, but it's some garbled HTML instead of the expected data. I've tried using NGROK to expose my localhost to the internet, and I tested the API call using Request Bin, where I can see that all the headers and parameters are being received correctly. The test in Request Bin passes without any issues. However, when I make the same call from Botpress, I'm still getting the strange HTML response. I've noticed that the request isn't hitting my local server because I'm running the application in npm run dev mode. I'm not sure what else to try at this point. Any ideas https://cdn.discordapp.com/attachments/1239034582528167978/1239034583178281020/image.png?ex=66417460&is=664022e0&hm=8fd7f67a9bcb0cc42127025bd698c8733b5e73cda3e137efa5c66da4ce97ee52&
New error related to CRIS Execute code "Send GET request to retrieve tools page from CREST website." { data: 'This API enables cross-origin requests to anywhere.\n\nUsage:\n\n/ Shows help\n/iscorsneeded This is the only resource on this host which is served without CORS headers.\n/ Create a request to , and includes CORS headers in the response.\n\nIf the protocol is omitted, it defaults to http (https if port 443 is specified).\n\nCookies are disabled and stripped from requests.\n\nRedirects are automatically followed. For debugging purposes, each followed redirect results\nin the addition of a X-CORS-Redirect-n header, where n starts at 1. These headers are not\naccessible by the XMLHttpRequest API.\nAfter 5 redirects, redirects are not followed any more. The redirect response is sent back\nto the browser, which can choose to follow the redirect (handled automatically by the browser).\n\nThe requested URL is available in the X-Request-URL response header.\nThe final URL, after following all redirects, is available in the X-Final-URL response header.\n\n\nTo prevent the use of the proxy for casual browsing, the API requires either the Origin\nor the X-Requested-With header to be set. To avoid unnecessary preflight (OPTIONS) requests,\nit\'s recommended to not manually set these headers in your code.\n\n\nDemo : https://robwu.nl/cors-anywhere.html\nSource code : https://github.com/Rob--W/cors-anywhere/\nDocumentation : https://github.com/Rob--W/cors-anywhere/#documentation\n',
status: 200, statusText: '', headers: { 'access-control-allow-origin': '*', 'cache-control': 'public, max-age=0, must-revalidate', 'content-type': 'text/plain' }, config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [ 'xhr', 'http' ], transformRequest: [ [Function] ], transformResponse: [ [Function] ], timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: { FormData: [Function: FormData], Blob: [Function: Blob] }, validateStatus: [Function: validateStatus], headers: { Accept: 'application/json, text/plain, */*', authorization: 'Bearer test', 'x-app-id': undefined, 'Content-Type': null }, withCredentials: false, baseURL: '', method: 'get', url: 'https://botpress.studio/api/proxy/%20%20https%3A%2F%2Fbotpress.studio%2Fapi%2Fproxy%2Fhttps%3A%2F%2Fcrest-website.vercel.app%2Fapi%2FgetPages%3Fpage%3Dtools', data: undefined }, request: { __sentry_xhr__: { method: 'GET', url: 'https://botpress.studio/api/proxy/%20%20https%3A%2F%2Fbotpress.studio%2Fapi%2Fproxy%2Fhttps%3A%2F%2Fcrest-website.vercel.app%2Fapi%2FgetPages%3Fpage%3Dtools', body: null, status_code: 200 } }
f
Hey there, Just to verify. It's actually running the function and you are getting something from it? I am asking because you are never invoking the function.
f
Hi i mean i didnt invoke in this example apologies but even i write like this it's not able to communicate var config = { method: 'GET', url: ' https://0c00-2405-6e00-31f0-7401-b9a6-1fa5-8a3c-a9ea.ngrok-free.app/api/getPages?page=tools', headers: { Accept: 'application/json', 'Content-Type': 'application/json' } } let response = await axios(config) console.log(response.data)
been working since morning to resolve this
I am not able to understand why
f
Thank you! Can you try and add 'X-Requested-With': 'XMLHttpRequest' to your header in Botpress
f
trying right now thank you
const url = ' https://0c00-2405-6e00-31f0-7401-b9a6-1fa5-8a3c-a9ea.ngrok-free.app/api/getPages?page=tools' const token = 'test' const headers = { Authorization:
Bearer ${token}
, 'X-Requested-With': 'XMLHttpRequest' // Add 'X-Requested-With' header } const response = await axios.get(url, { headers: headers }) // Use the response data as needed const data = response.data // ... rest of the code ... console.log(data)
still same error
f
Not sure to be honest. Might be an issue with a Botpress proxy, although I am a bit lost.
f
Thank you Decay means a lot
f
You are very welcome! Hopefully the Botpress team can help us more in the upcoming week
b
HEY @fierce-zebra-31242 @User I'm also stuck, probably because of the proxy. Did you find a solution?
3 Views