Comments (6)
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.
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.
@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.
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.
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.
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)
- Is there a way to send client-certificates with a request? HOT 2
- implement useSessionCookies flag HOT 1
- when using fetch in renderer process then it is giving me errors HOT 5
- support AbortSignal which `node-fetch` supported! HOT 2
- v1.7.0 made "signal" option required in TS declaration file - please make it optional HOT 1
- checkInvalidHeaderChar throws if header contains emoji HOT 5
- can not read header content-type if useElectronNet is true HOT 11
- Agent option silently ignored when useElectronNet is enabled HOT 1
- TypeScript & esbuild try to resolve 'electron' even not in electron HOT 4
- Named pipe support on Windows? HOT 6
- Type Defintions for `fetch` should take `RequestInfo` isntead of `string`
- `blob.type` is empty for certain image urls HOT 7
- Consider using undici? HOT 1
- Throws instead of rejecting the promise if a sent header value is invalid HOT 1
- FetchError typings: special case for 'system' HOT 3
- Basic auth-scheme issue HOT 1
- How to implement keepalive? HOT 1
- [solved] Get request headers before fetch? HOT 3
- Does not Work Correctly HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electron-fetch.