Script don't work in Chrome, but works in preview and test on Chrome. (SSL handshake failed)


#1

I’m trying out some different ways to connect to external API’s, like the Cat API and Httpbin…

But on Chrome Version 67.0.3396.99 (MacOS), i keep getting a request-loop - hundreds of requests until i close the tab.

Every time with the error: SSL handshake failed

The scripts i tried:

addEventListener('fetch', event => {

  // This handler is for /cats only.
  let url = new URL(event.request.url)
  if (url.pathname.startsWith("/cats")) {
    event.respondWith(getCats(event.request))
  } else {
    return;
  }
})

async function getCats(request) {

  let url = 'http://thecatapi.com/api/images/get?format=xml&results_per_page=1'
  let headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }

  const init = {
    method: 'GET',
    headers: headers
  }
  const response = await fetch(url, init)
  console.log('Got response', response)
  return response

}

And:

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

async function fetchAndApply(request) {
  const response = await fetch('https://httpbin.org/xml')
  const modifiedHeaders = new Headers(response.headers)

  modifiedHeaders.set('Content-Type', 'text/xml')

  const init = {
    status: response.status,
    statusText: response.statusText,
    headers: modifiedHeaders
  }
  const modifiedResponse = new Response(response.body, init)
  return modifiedResponse
}

#2

Nevermind, had to set SSL setting to “Full” for it to work.