Skip to content
Advertisement

TypeError: Incorrect type for the ‘headers’ field on ‘RequestInitializerDict’: the provided value is not of type ‘variant’

I’m new to JavaScript and I’m trying make a Github API Gateway for IFTTT(cause it can’t modify header) with JS on Cloudflare Worker. Here’s the code:

async function handleRequest(request) {
    var url = new URL(request.url)
    var apiUrl = 'https://api.github.com' + url.pathname
    var basicHeaders = {
        'User-Agent': 'cloudflare',
        'Accept': 'application/vnd.github.v3+json'
    }


    const { headers } = request
    const contentType = headers.get('content-type')
    const contentTypeUsed = !(!contentType)

    if (request.method == 'POST' && contentTypeUsed) {
        if (contentType.includes('application/json')) {
            var body = await request.json()

            if ('additionHeaders' in body) {
                var additionHeaders = body.additionHeaders
                delete body.additionHeaders
            }

            var apiRequest = {
                'headers': JSON.stringify(Object.assign(basicHeaders,additionHeaders)),
                'body': JSON.stringify(body),
            }

        } else {
            return new Response('Error: Content-Type must be json', {status: 403})
        }

        const newRequest = new Request(apiUrl, new Request(request, apiRequest))

        try {
            var response = await fetch(newRequest)
            return response
        } catch (e) {
            return new Response(JSON.stringify({error: e.message}), {status: 500})
        }

    } else {
        var apiRequest = {
            'headers': JSON.stringify(basicHeaders)
        }
        const newRequest = new Request(apiUrl, new Request(request, apiRequest))
        var response = await fetch(newRequest)
        return response
    }
}

addEventListener('fetch', async (event) => {
    event.respondWith(handleRequest(event.request))
})

And I got this error when I tried to run it:

Uncaught (in promise)
TypeError: Incorrect type for the 'headers' field on 'RequestInitializerDict': the provided value is not of type 'variant'.
    at worker.js:1:1245
    at worker.js:1:1705
Uncaught (in response)
TypeError: Incorrect type for the 'headers' field on 'RequestInitializerDict': the provided value is not of type 'variant'.

This is an older version which run well but with less flexibility:

async function handleRequest(request) {
    var url = new URL(request.url)
    var apiUrl = 'https://api.github.com' + url.pathname
    var accessToken = 'token '

    var apiRequest = {
        headers: {
            'User-Agent': 'cloudflare',
            'Accept': 'application/vnd.github.v3+json'
        }
    }

    const { headers } = request
    const contentType = headers.get('content-type')
    const contentTypeUsed = !(!contentType)

    if (request.method == 'POST' && contentTypeUsed) {
        if (contentType.includes('application/json')) {
            var body = await request.json()

            if ('token' in body) {
                accessToken += body.token
                delete body.token
            }

            var apiRequest = {
                headers: {
                    'Authorization': accessToken,
                    'User-Agent': 'cloudflare',
                    'Accept': 'application/vnd.github.v3+json'
                },
                body: JSON.stringify(body),
            }

        } else {
            return new Response('Error: Content-Type must be json', {status: 403})
        }

        const newRequest = new Request(apiUrl, new Request(request, apiRequest))

        try {
            var response = await fetch(newRequest)
            return response
        } catch (e) {
            return new Response(JSON.stringify({error: e.message}), {status: 500})
        }

    } else {
        const newRequest = new Request(apiUrl, new Request(request, apiRequest))

        var response = await fetch(newRequest)
        return response
    }
}

addEventListener('fetch', async (event) => {
    event.respondWith(handleRequest(event.request))
})

The only difference seems to be apiRequest, but I don’t know how to fix it. I tried to claim the variable with var apiRequest = new Object() first but didn’t work.

Advertisement

Answer

Fix with this:

let apiRequest = new Object
apiRequest.headers = Object.assign(basicHeaders, additionHeaders)
apiRequest.body = JSON.stringify(body)

And the apiRequest will look like this:

{headers:{},body:"{}"}

This seems like what RequestInitializerDict want.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement