R
Railway8mo ago
4tendev

fetching from front in private network but getting error

this is my fetch function async function getData() { const userCookies = cookies() const url = process.env.INTERNAL_BACK_URL ??"http://127.0.0.1:8000/" const res = await fetch(url+"user/info/", {headers: { Cookie : userCookies },}) if (!res.ok) { throw new Error('Failed to fetch data') }
return res.json() } and for url i used this 2 pattern but both get errors pergamer_back.railway.internal/user/info [cause]: TypeError [ERR_INVALID_URL]: Invalid URL and http://pergamer_back.railway.internal/ TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:11576:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { cause: Error: connect ECONNREFUSED fd12:ae40:cb28::cf:ac56:6757:80 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: 'fd12:ae40:cb28::cf:ac56:6757', port: 80
75 Replies
Percy
Percy8mo ago
Project ID: 23052b53-336e-48e9-8fa0-9a094c4b4233
4tendev
4tendev8mo ago
23052b53-336e-48e9-8fa0-9a094c4b4233
Brody
Brody8mo ago
what kind of frontend app is this?
4tendev
4tendev8mo ago
next.js
Brody
Brody8mo ago
and you are making the request from the backend of next, correct?
4tendev
4tendev8mo ago
yes for serverside fetching works fine in local
Brody
Brody8mo ago
okay cool, and what kind of app are you trying to make a request to?
4tendev
4tendev8mo ago
Django
Brody
Brody8mo ago
can you access the django app from the railway public domain?
4tendev
4tendev8mo ago
yes if remove fetch in serverside in front fetching works fine
Brody
Brody8mo ago
are you running the django app with gunicorn?
4tendev
4tendev8mo ago
yes
Brody
Brody8mo ago
give me your current start command please
4tendev
4tendev8mo ago
CMD python manage.py makemigrations && python manage.py migrate && gunicorn --bind 0.0.0.0:80 core.wsgi:application
Brody
Brody8mo ago
change it to
python manage.py makemigrations && python manage.py migrate && gunicorn -b [::]:$PORT core.wsgi:application
python manage.py makemigrations && python manage.py migrate && gunicorn -b [::]:$PORT core.wsgi:application
and then delete your PORT service variable
4tendev
4tendev8mo ago
and do i need set cors orgin for it if yes waht url is it ?
Brody
Brody8mo ago
if you didn't need cors before you don't need it now
4tendev
4tendev8mo ago
TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:11576:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { cause: Error: connect ECONNREFUSED fd12:ae40:cb28::2e:6608:4983:80 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: 'fd12:ae40:cb28::2e:6608:4983', port: 80 } } [Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] { digest: '956933652' } do i need restart my front also ?
Brody
Brody8mo ago
send me the deployment logs for the django service please https://bookmarklets.up.railway.app/log-downloader/
4tendev
4tendev8mo ago
sry i could use the link u gave couldnt* is it enough ? https://api.forbtc.org/user/info/ with this link u can get json response
Brody
Brody8mo ago
what is INTERNAL_BACK_URL set to?
4tendev
4tendev8mo ago
http://pergamer_back.railway.internal/ it was without http but i got bad url but i didnt test with new setup u gave for backend
Brody
Brody8mo ago
okay, show me a full screenshot of your railway project now please
4tendev
4tendev8mo ago
using screen shot button ?
Brody
Brody8mo ago
whatever method you would like as long as the screenshot shows the entire browser window
4tendev
4tendev8mo ago
No description
Brody
Brody8mo ago
set a PORT variable on the django service to 8000
4tendev
4tendev8mo ago
TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:11576:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { cause: Error: connect ECONNREFUSED fd12:ae40:cb28::11:ceeb:11a2:80 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: 'fd12:ae40:cb28::11:ceeb:11a2', port: 80 } } still the same
Brody
Brody8mo ago
we arent done yet dont worry also, please only use that log downloader when sending your logs
4tendev
4tendev8mo ago
Ok 👍
Brody
Brody8mo ago
change your INTERNAL_BACK_URL variable to http://${{pergamer_back.RAILWAY_PRIVATE_DOMAIN}}:${{pergamer_back.PORT}} make sure to cancel the deployment popup once done, click the eye icon and send me the resulting string
Brody
Brody8mo ago
my variable does not have a trailing slash, why does the resulting string have a trailing slash
4tendev
4tendev8mo ago
at end ?
Brody
Brody8mo ago
yes, did you add a slash at the end?
4tendev
4tendev8mo ago
i need end slash to works
Brody
Brody8mo ago
you shouldnt, please keep the variable as i have said
4tendev
4tendev8mo ago
Ok
Brody
Brody8mo ago
okay now you can go ahead and restart the next deployment, 3 dot menu on the active deployment
4tendev
4tendev8mo ago
ok should edit my url so add slash before the end point ?
Brody
Brody8mo ago
i dont see why you would need it
4tendev
4tendev8mo ago
const url = process.env.INTERNAL_BACK_URL ??"http://127.0.0.1:8000/" const res = await fetch(url+"user/info/", after port
Brody
Brody8mo ago
do this then url + "/user/info/"
4tendev
4tendev8mo ago
some thing changed but not working
Brody
Brody8mo ago
well thats a different error, so that means progress do this again for me please
4tendev
4tendev8mo ago
Brody
Brody8mo ago
no trailing slash!
4tendev
4tendev8mo ago
it doent have slash but when copy it aperas http://pergamer_back.railway.internal:8000
Brody
Brody8mo ago
okay show me in screenshot please
4tendev
4tendev8mo ago
No description
Brody
Brody8mo ago
cool, show me the new fetch code then please, and enclose the code in triple back ticks please
4tendev
4tendev8mo ago
async function getData() { const userCookies = cookies() const url = process.env.INTERNAL_BACK_URL ??"http://127.0.0.1:8000" const res = await fetch(url+"/user/info/", {headers: { Cookie : userCookies },}) if (!res.ok) { throw new Error('Failed to fetch data') }
return res.json() }
Brody
Brody8mo ago
okay then make next give us a better error printout also, you can still access the django service from the public domain right?
4tendev
4tendev8mo ago
yes
Brody
Brody8mo ago
let me know when you have made next give a better error printout
4tendev
4tendev8mo ago
i dont get this
Brody
Brody8mo ago
Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. make next give us the actually error
4tendev
4tendev8mo ago
that the actual error (this part shows in browser) but the logs i sent is actual record
Brody
Brody8mo ago
as the error message states, it is most certainly not the actual error
4tendev
4tendev8mo ago
if you check logs it has ending that adres to the browser error the last part of it
Brody
Brody8mo ago
there is no useful information here, as ive said, make next give us the actual error
4tendev
4tendev8mo ago
for example in browser : Application error: a server-side exception has occurred (see the server logs for more information). Digest: 2964946319 in server log Error: Failed to fetch data at getData (/app/.next/server/chunks/145.js:1:11775) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async User (/app/.next/server/chunks/145.js:1:11846) [Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] { digest: '2964946319' } it's joint so you adress the error to find real log in seerver
Brody
Brody8mo ago
let me know when you are able to configure next to print the actual error
4tendev
4tendev8mo ago
it's actual error im sure about it
Brody
Brody8mo ago
there is no useful information here, as ive said, make next give us the actual error without an actual error, i cant help
4tendev
4tendev8mo ago
Ok i dont know how but i will try tommorw if get new kind of error ty for help anyway really patient man/women
Brody
Brody8mo ago
no problem (man) 🤣
4tendev
4tendev8mo ago
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'pergamer_back.railway.internal:8000'. The domain name provided is not valid according to RFC 1034/1035. oh now django dont accept it
Brody
Brody8mo ago
time for you to read the django docs 🙂
4tendev
4tendev8mo ago
actualy after error changed now next can connect but django doesnt accept to call by that URL
Brody
Brody8mo ago
^
4tendev
4tendev8mo ago
Fixed railway use the default github name for the internal url but django doesnt accept the any character in the url so i needed the change the internal url to fix last part
Brody
Brody8mo ago
awesome!