Next.js Discord

Discord Forum

Niche Fetch Issue

Answered
Jboncz posted this in #help-forum
Open in Discord
I have an archaic system that we utilize at work. I have to fetch a token to interact with it via its REST interface. The issue is the endpoint you auth to returns a 405, even though it works as expected (I have filed a defect with IBM and will eventually get fixed...) in the mean time, im having issues getting cookie thats provided when I authenticate because of the way the fetch() api works.

From what I can tell in nodejs when a 40x error gets received it is not returning any cookies that came with the response. I can provide this by hitting the same endpoint with cURL and get the cookie response. Is there any way to force the fetch API to return this token?
Answered by Jboncz
Im going to have to invoke cURL from within nodejs to get the response I need. sadge
View full answer

102 Replies

Masai Lion
interesting
alr
I need that damn Lpta token man.
Masai Lion
you sure that’s the right path to the uhhh
your api address?
Yeah its the same in both screenshots
Its 100% correc
110%
Masai Lion
Alr cuz it says 404 error
Which means not found
:meow_stare:
As I said, its a defect.
Notice, that the curl command gets a token
where as the nodejs fetch doesnt.
@Jboncz Notice, that the curl command gets a token
Masai Lion
true true
alr
Same issue, same product
Masai Lion
give me a few minutes to think on a solution
I just actually made a fuss about it lol
Masai Lion
I think it might be something related to the way you fetching
Im all ears. Unfortunately theres no way for me to provide a practical example as its an internal applicaiton
ye do
Nws
Give me a few minutes
async function getData() {
 const res = await fetch('<https://api.example.com/...'>)
 
 if (!res.ok) {
 throw new Error('Failed to fetch data')
 }
 
 return res.json()
}
 
export default async function Page() {
 const data = await getData()
 
 return <main></main>
}
You need auth right @Jboncz ?
Something like my PayPal Fetch….
Yeah, thats what the other stuff is.
Masai Lion
Ohhh so then I can just share my PayPal fetch, and then you can just like, replace the api route and the values
const url = 'https://accessrequest.comp.org:9082/itim/j_security_check';
    const formData = new URLSearchParams();
    formData.append('j_username', 'Jboncz');
    formData.append('j_password', pw);

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: formData,
    })
        .then(response => {
            console.log(response.headers);
            console.log(response.status);
            console.log(response.statusText);
            // handle response
        })
        .catch(error => {
            console.error('Error:', error);
        });
Lol its not going to work.
@Jboncz Lol its not going to work.
Masai Lion
:meow_stare:
because the paypal API actually returns a not 40x message
@Jboncz because the paypal API actually returns a not 40x message
Masai Lion
And you are requiring a 2xx?
Nooo, its expected to return a 404, but its returns a 404 with a cookie with the authentication token.
I just cant see the cookie in nodejs
even though its there
Masai Lion
Dayum
Alr
cause nodejs says 'its an error, we dont want the cookie'
Its okay man, I was only messing around, you dont gotta solve this issue. 🙂
@Jboncz Its okay man, I was only messing around, you dont gotta solve this issue. 🙂
Masai Lion
nahh nws, I want to try help u
@Jboncz cause nodejs says 'its an error, we dont want the cookie'
Masai Lion
so it’s nodejs skill issue
Yes, agreed. lol
Masai Lion
We have to somehow tell code to not get the cookie
force it
:meow_stare:
Yeah, I looked through all the fetch api documentation lol
I find nothing worth trying
Masai Lion
Do
So*
credentials: 'omit',
We might need to add this
Inside your fetch
I tried all three options 😂
Masai Lion
fetch(url, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: formData,
    credentials: 'omit', // hopefully we can force with this to say Nuh uh to cookie
})
@Jboncz I tried all three options 😂
Masai Lion
dayum you tried already?
omit, same-origin, and include
@Jboncz omit, same-origin, and include
Masai Lion
Breh
Its okay man. I dont think there is a solution
tbh
Masai Lion
We might need to delete the cookie manually
const filteredHeaders = response.headers.raw();
delete filteredHeaders['set-cookie'];
:meow_stare:
There is no cookie to delete, its null
@Jboncz There is no cookie to delete, its `null`
Masai Lion
:meow_stare: :meow_stare: :meow_stare:
i don’t have any idea left…
try removing the cache from your site….
Its okay bud, I apprecaite you trying
Masai Lion
maybe
Ill give it a shot
@Jboncz Its okay bud, I apprecaite you trying
Masai Lion
I really wanted to help :meow_stare:
@Jboncz Ill give it a shot
Masai Lion
That was happening with me, Redux was having skill issues until I deleted the unnecessary web cache
Its all in linux, and the service has been restarted a few times, so no caching
American Crow
i know it doesnt make sense. just a wrapper same node runtime. But i got that weird paranoia feeling. If are desperate enough try once with ky just for the lulz:
await ky.get('http://localhost:4000/set-session', { cache: 'no-store', credentials: 'include' })
What is ky? lol
Oh I see. lol
npm package. ill give it a shot why not
American Crow
oh sorry a thin wrapper for the native fetch https://github.com/sindresorhus/ky
Yeah nextjs is providing you with a wrapper too, they might be doing something funky. Ill give it a shot
American Crow
yea thats my weird feeling. Prob not but maybe 5% chance
@American Crow yea thats my weird feeling. Prob not but maybe 5% chance
Masai Lion
That’s a good idea tbh
To wrap the fetch
:thinq:
American Crow
kyin general is great
its like having a native fetch but with the power of interceptors, request cancellation, fetch intervals, a modern api
it integrates with nextjs patched fetch since its just a thin wrapper
I doubt it can solve this though :fine:
Yeah... it may be a intended security feature of the primative fetch call.
it makes sense, dont return cookies if its 404
Im going to have to invoke cURL from within nodejs to get the response I need. sadge
Answer
I do like ky though, the hooks functionality is pretty sick
I tried to hook into it before it threw the error, but alas
no luck
maybe someone will come along with a solution we will see.
Masai Lion
bumping
@Masai Lion bud, going out of your way to do something that I told you bothered me is not very cool (using the large text for no reason).

I am good I dont need help bumping a post.