Code Monkey home page Code Monkey logo

Comments (6)

arantes555 avatar arantes555 commented on June 13, 2024

What makes you believe that a fetch to https://www.google.com should actually return a 307 redirect ? When trying with curl, it actually returns a 200 for me, so I am not sure what you're trying to do.

from electron-fetch.

jkpadmanapan avatar jkpadmanapan commented on June 13, 2024

Thanks for your immediate reply. Pls try with http. I think you have tried with HTTPS. You will get 307 response if you try http://www.google.com. i am mentioning google for your understanding. Actually there are lot of websites deal with redirection.

from electron-fetch.

arantes555 avatar arantes555 commented on June 13, 2024

@jkpadmanapan I can confirm that I also get a 200 with http

➜  ~  curl http://www.google.com -I
HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Date: Sun, 27 Sep 2020 16:35:21 GMT
Server: gws
X-XSS-Protection: 0
...

electron-fetch's behaviour on redirects is quite thoroughly tested, I really do not think there's a bug in it. If electron-fetch (in manual mode) gives you a 200, it's actually a 200.

from electron-fetch.

jkpadmanapan avatar jkpadmanapan commented on June 13, 2024

Thanks again. Now i came back with node - fetch example.. Pls check the below

I am going to hit http://github.com/ using node-fetch with manual redirect

const fetch = require('node-fetch');

fetch('http://github.com/', { method: 'GET', redirect : 'manual' })
.then(res => console.log("response status --->",res.status))

Output :
response status ---> 301

Now i am going to hit the same github url, using electron-fetch with manual redirect.

const { app, net, BrowserWindow } = require("electron");
const url = require("url");
const path = require("path");
const fetch = require("electron-fetch").default;
app.whenReady().then(() => {
let window = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: { nodeIntegration: true, webviewTag: true },
resizable: false,
show: false,
});

let session = window.webContents.session;
session.clearCache();
session.clearAuthCache();

// session.webRequest.onBeforeRequest((details, callback) => {
// callback({ cancel: true })
// })

var body = {
method : "GET",
redirect : "manual"
}
const request = fetch("http://github.com/", body)
.then(async function (response) {
// var text = await response.text();
console.log("electron-fetch response -->",response.status);

})
.catch(function (err) {
console.log("error --", err);
});
});

Output :

electron-fetch response --> 200

So what i conclude is electron-fetch is automatically does redirect. It should have manual redirect.

Can you please check the above example ?

from electron-fetch.

arantes555 avatar arantes555 commented on June 13, 2024

Sorry for earlier. You are right, the redirect option does not work when running on Electron. It works when electron-fetch is running in node, but not on electron. It's actually documented in electron-fetch's readme : the redirect option is noted as only works when running on Node.js.

It was a limitation of Electron's net API when this was implemented, but it appears that they have now added the option to control how redirects are followed ( https://www.electronjs.org/docs/api/client-request#new-clientrequestoptions ), so it would be possible to implement it now.

A PR to implement it would be most welcome :)

from electron-fetch.

jkpadmanapan avatar jkpadmanapan commented on June 13, 2024

Okay. I just made it work by adding redirect event in index.js under electron-fetch/lib, after line number 1404.

here is the code snippet.

req.on("redirect", (statusCode, method, redirectUrl,responseHeaders ) => {
if ( request.redirect == 'follow' ) {
req.followRedirect();
}
else {
const responseOptions_redir = { url: request.url,
status: statusCode,
statusText: statusCode,
headers: responseHeaders ,
size: request.size,
timeout: request.timeout,
useElectronNet: request.useElectronNet,
useSessionCookies: request.useSessionCookies
}
const response_redir = new Response(null, responseOptions_redir);
req.abort();
resolve(response_redir);
}
});

can you please check the code? I think you can improve this code..

from electron-fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.