Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var fetch = require("node-fetch");
exports.handler = async (event, context) => {
var url = event.path;
url = url.split(".netlify/functions/cors-binary/")[1];
url = decodeURIComponent(url);
url = new URL(url);
for (let i in event.queryStringParameters) {
url.searchParams.append(i, event.queryStringParameters[i]);
}
console.log(url.href);
var cookie_string = event.headers.cookie || "";
var useragent = event.headers["user-agent"] || "";
var header_to_send= {
"Cookie": cookie_string,
"User-Agent": useragent,
"content-type": "application/json",
"accept": "*/*",
"host": url.host
};
var options = {
method: event.httpMethod.toUpperCase(),
headers: header_to_send,
body: event.body
}
if (event.httpMethod.toUpperCase() == "GET" || event.httpMethod.toUpperCase() == "HEAD") delete options.body;
var response = await fetch(url, options);
var response_buffer = await response.buffer();
var base64_encoded = response_buffer.toString("base64");
var headers = response.headers.raw();
var cookie_header = null;
if (headers["set-cookie"]) cookie_header = headers["set-cookie"];
return {
statusCode: 200,
isBase64Encoded: true,
body: base64_encoded,
headers: {
"content-type": String(headers["content-type"]) || "text/plain"
},
multiValueHeaders: {
"set-cookie": cookie_header || []
}
}
};
var fetch = require("node-fetch");
exports.handler = async (event, context) => {
var url = event.path;
url = url.split(".netlify/functions/cors/")[1];
url = decodeURIComponent(url);
url = new URL(url);
for (let i in event.queryStringParameters) {
url.searchParams.append(i, event.queryStringParameters[i]);
}
console.log(url.href);
var cookie_string = event.headers.cookie || "";
var useragent = event.headers["user-agent"] || "";
var header_to_send= {
"Cookie": cookie_string,
"User-Agent": useragent,
"content-type": "application/json",
"accept": "*/*",
"host": url.host
};
var options = {
method: event.httpMethod.toUpperCase(),
headers: header_to_send,
body: event.body
}
if (event.httpMethod.toUpperCase() == "GET" || event.httpMethod.toUpperCase() == "HEAD") delete options.body;
var response = await fetch(url, options);
var response_text = await response.text();
var headers = response.headers.raw();
var cookie_header = null;
if (headers["set-cookie"]) cookie_header = headers["set-cookie"];
return {
statusCode: 200,
body: response_text,
headers: {
"content-type": String(headers["content-type"]) || "text/plain"
},
multiValueHeaders: {
"set-cookie": cookie_header || []
}
}
};
@sanatg
Copy link

sanatg commented Nov 1, 2021

awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment