Next.js Discord

Discord Forum

expiremental-https fetching on api causes error ?

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
So when I'm calling my api directly from a server component I get no error, just 401 unauthorized which is expected because my api (written in Go) is in prod already, and I am using local host for dev, I am obviously unable to use the cookie in a server component because the cookie cannot be set in the browser on localhost if my api is in production.

So I made a route handler at /api/redirect to get the cookie from an endpoint from, set it, then call the actual endpoint I want. I am getting something like this:

 
⨯ TypeError: fetch failed

  cause: [Error: 401C67DE01000000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1586:SSL alert number 80
  ] {
    library: 'SSL routines',
    reason: 'tlsv1 alert internal error',
    code: 'ERR_SSL_TLSV1_ALERT_INTERNAL_ERROR'
  }
}


This is my code with the api endpoint removed for obvious reasons

2 Replies

Sun bearOP
async function handleRequest(method: string, req: NextRequest) {
    console.log("handleRequest")
    // fetch from api.viewthis.app/api/cookie
    try {
        const cookieResponse = await fetch(
            "https://{API_ENDPOINT}/api/cookie",
            {
                method: "GET",
                headers: req.headers,
            }
        )
        console.log("cookieResponse", cookieResponse)

        const cookieData = await cookieResponse.json()
        console.log(cookieData)
    } catch (e) {
        console.log(e)
        return NextResponse.json({ error: "error" })
    }
    const searchParams = req.nextUrl.searchParams
    const redirectUrl = searchParams.has("path")
        ? `{API_ENDPOINT}/api/${searchParams.get("path")}`
        : "{API_ENDPOINT}"
    console.log("redirectUrl", redirectUrl)

    return NextResponse.json(["noError"])
}


It returns { error: error}

Again, calling the api in the server component works! Which is good, but not on localhost because of the cookies.
Calling the api from this route handler causes that error.

I replaced API_ENDPOINT with the actual endpoint in prod.

I am runnning bun dev --experimental-https.
Sun bearOP
Something interesting is that it works when i make the call to https://127.0.0.1:3000/api/redirect?path= and not https://localhost:3000/api/redirect?path=