Error 400: Bad request (with cloudflare in the response body)

Hi, when I try to visit a website I get a 400 bad request response! But it has “cloudflare” in the return response body! This is what I see


Is there something wrong with Cloudflare or what the ■■■■ is going on?

Yes, this is on a worker! Also when I send a “GET” request, the following information is returned (with sensitive information like the true IP redacted)
“outcome”: “ok”,
“scriptName”: null,
“exceptions”: ,
“logs”: ,
“eventTimestamp”: 1634828122561,
“event”: {
“request”: {
“url”: “https://no-header-host.testcoding.workers.dev/”,
“method”: “GET”,
“headers”: {
“accept”: “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9”,
“accept-encoding”: “gzip”,
“accept-language”: “en-US,en;q=0.9”,
“cf-connecting-ip”: {Redacted},
“cf-ipcountry”: {Redacted},
“cf-ray”: “6a1b5395fdec0f16”,
“cf-visitor”: “{“scheme”:“https”}”,
“connection”: “Keep-Alive”,
“dnt”: “1”,
“host”: “no-header-host.testcoding.workers.dev”,
“sec-ch-ua”: "“Chromium”;v=“94”, “Google Chrome”;v=“94”, “;Not A Brand”;v=“99"”,
“sec-ch-ua-mobile”: “?0”,
“sec-ch-ua-platform”: ““Chrome OS””,
“sec-fetch-dest”: “document”,
“sec-fetch-mode”: “navigate”,
“sec-fetch-site”: “cross-site”,
“sec-fetch-user”: “?1”,
“upgrade-insecure-requests”: “1”,
“user-agent”: “Mozilla/5.0 (X11; CrOS x86_64 14150.57.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.97 Safari/537.36”,
“x-forwarded-proto”: “https”,
“x-real-ip”: {Redacted}
},
“cf”: {
“longitude”: {Redacted},
“latitude”: "{Redacted},
“tlsCipher”: “AEAD-AES128-GCM-SHA256”,
“continent”: “NA”,
“asn”: 11492,
“clientAcceptEncoding”: “gzip, deflate, br”,
“country”: "{Redacted},
“tlsClientAuth”: {
“certIssuerDNLegacy”: “”,
“certIssuerSKI”: “”,
“certSubjectDNRFC2253”: “”,
“certSubjectDNLegacy”: “”,
“certFingerprintSHA256”: “”,
“certNotBefore”: “”,
“certSKI”: “”,
“certSerial”: “”,
“certIssuerDN”: “”,
“certVerified”: “NONE”,
“certNotAfter”: “”,
“certSubjectDN”: “”,
“certPresented”: “0”,
“certRevoked”: “0”,
“certIssuerSerial”: “”,
“certIssuerDNRFC2253”: “”,
“certFingerprintSHA1”: “”
},
“tlsExportedAuthenticator”: {
“clientFinished”: “75106af4e2d475db98b757e0e8b164cbf303e718b377a34aa14eb419b091ddec”,
“clientHandshake”: “6fea5d8aeaa1478bdba715468f7c75e24595e4ae8583583189d669ee3ded3cfd”,
“serverHandshake”: “cb19608e31648471a7d617633b79203dea53e9f8dce22c197a8a9ab837513ee8”,
“serverFinished”: “f6941eac1b8449cd81bdef751c28ff34d5c856b67cecb40152717aacbe429e4f”
},
“tlsVersion”: “TLSv1.3”,
“colo”: “DFW”,
“timezone”: {Redacted},
“city”: {Redacted},
“edgeRequestKeepAliveStatus”: 1,
“requestPriority”: “”,
“httpProtocol”: “HTTP/3”,
“region”: {Redacted},
“regionCode”: {Redacted},
“asOrganization”: {Redacted},
“metroCode”: {Redacted},
“postalCode”: {Redacted}
}
}
},
“id”: 0
}

Can you share the code with which you’re having this issue?
Preferably in a readable formatted code-block

addEventListener(“fetch”, (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});

/**

if (pathname.startsWith("/api")) {
return new Response(JSON.stringify({ pathname }), {
headers: { “Content-Type”: “application/json” },
});
}

if (pathname.startsWith("/status")) {
const httpStatusCode = Number(pathname.split("/")[2]);

return Number.isInteger(httpStatusCode)
  ? fetch("https://nameless-silence-22c7.testcoding.workers.dev//" + httpStatusCode)
  : new Response("That's not a valid HTTP status code.");

}

return fetch(“https://”);
}

The code you currently posted is not really readable.
You can use the formatting option to better format your code and make it more readable
image

addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

/**
 * Many more examples available at:
 *   https://developers.cloudflare.com/workers/examples
 * @param {Request} request
 * @returns {Promise<Response>}
 */
async function handleRequest(request) {
  const { pathname } = new URL(request.url);

  if (pathname.startsWith("/api")) {
    return new Response(JSON.stringify({ pathname }), {
      headers: { "Content-Type": "application/json" },
    });
  }

  if (pathname.startsWith("/status")) {
    const httpStatusCode = Number(pathname.split("/")[2]);

    return Number.isInteger(httpStatusCode)
      ? fetch("https://nameless-silence-22c7.testcoding.workers.dev//" + httpStatusCode)
      : new Response("That's not a valid HTTP status code.");
  }

  return fetch("https://");
}

By default you’re trying to fetch and return a incorrect URL, which is why you’re seeing a 400 error.
You should change it to a correct URL to fix that issue.

So change the fetch and return URL’s? Will do!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.