-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: HTTP API #2495
WIP: HTTP API #2495
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a minor comment
Also, use CAPLog.print
instead of print
so it can be disabled by the user, unless you plan to remove them before merging.
Other than that, looks good to me.
This is great news! I posted in the other thread, but this may be helpful when you run into edge cases: https://github.com/facebook/react-native/tree/master/Libraries/Network I don't speak Swift, so I'm not sure if there are any parallels. But I'm sure they've dealt with a few quirks already. The only think worth mentioning, with myself included, is that cookie support was another big selling point of native HTTP. I hope that's the case with this new plugin. Thanks for doing this! Huge step forward for Capacitor. |
Thanks @silviogutierrez just added Cookie support. From what I gather from your link and the advanced HTTP plugin, being able to set a cookie for a URL (future requests for this URL will then send the cookie) and getting all cookies for a URL is sufficient? |
@mlynch I didn't realize that my team had this package private: https://github.com/TeamHive/capacitor-http Unsure if it'll be of any help or not, but figured I'd share as it's timely 👍 |
@mlynch : that's awesome! Thanks. I don't specifically set cookies for URLs directly. Rather, I just call API endpoints and they set cookies and get stored automagically. That's how the current native plugin does it. My own dream would be to make everything transparent, but then it wouldn't really parallel fetch and other systems. That is, Let me decide what cookies to send with every request, just as I do headers, params, etc. But then things get wonky with expirations, honoring server overrides, etc. So I think as long as its close to the fetch spec, all is well. I really hope, like Fetch, this supports sending json and form/multipart data. Not all endpoints receive JSON after all. Here is my shoddy abstraction around the current native plugin to make it closer to fetch. The interesting parts are around handling where the Cordova plugin triggers an exception whereas fetch does not. Hopefully it helps: // I use a special POST_FORM type literal to make mine auto serialize form data, but ignore that if you want.
try {
const headers = {/*CORDOVA PLUGIN FIZZLES OUT WITH UNDEFINED VALUES FOR HEADERS, FETCH DOES NOT */};
const url = ''; // build URL manually injecting GET params so that it's closer to fetch. Can provide code.
const response = await HTTP.sendRequest(url, {
method:
method === "POST_FORM" ? "post" : (method.toLowerCase() as "get"),
data: "payload" in request ? request.payload : {},
serializer: method === "POST_FORM" ? "urlencoded" : "json",
headers,
});
const data =
response.headers["content-type"] === "application/json"
? JSON.parse(response.data)
: response.data;
const responseHeaders = new Headers(response.headers);
return {
data,
headers: responseHeaders,
};
} catch (error) {
const headers = new Headers(error.headers ?? {});
if (error.status == 400) {
const data = JSON.parse(error.error);
return {
status: 400,
errors: data,
headers,
};
} else if (
error.status == 302 ||
error.status == 301 ||
error.status == 403 ||
error.status == 404
) {
const headers = new Headers(error.headers);
return {
status: error.status,
headers,
};
} else if (error.status == 401) {
return {
status: 401,
headers,
};
}
return {
error: true,
message: `${error}`,
details: JSON.stringify(error),
};
} |
Anyone up to help test? So far on iOS this is working: Get/send json using |
@mlynch : Happy to test. I rely on my login endpoint setting a cookie, so I'd need one of:
With any of the above I can give it a full rundown. Thanks again for all this work! |
@nguyenduclong-ict yes it will work in the browser during serve. @CaptJakk got an example of what that would look like? Happy to have something like that in here but would want to make sure it's pretty generic and only uses core APIs |
we would add optional parameters to the http request and delegate the socks initiation to the underlying java and swift libs. specifically using URLSessionConfiguration for swift and HttpURLConnection for java has an overloaded constructor that will take a socks proxy. Ideally we'd make it as an optional final parameter to the 'request' call. For the record Socks5 is very generic, it is just a way to proxy tcp traffic through another party. We plan on using this for Tor specifically but there are use cases beyond that. There is also precedent for things like this in axios on nodejs etc. The way we would propose adding this to the http api is making an optional parameter to a request containing the proxy information that would default to no proxy if it was not supplied, this way it would not affect the API for any users who did not care about it. |
I'm starting a new project and having this would simplify our design a lot, since we need background mode network access and don't want to use the 'advanced' http plugin. I can defer us having to build the network access code for a couple of weeks. What do we think the ETA for this making it into a main Capacitor release is? I hate asking, but are we talking a couple of weeks or a couple of months? This will be a massive step in the right direction for Capacitor! Native web access would speed up so many things in Ionic apps. |
We’ve had some success factoring this plugin into a standalone capacitor plugin for our project, so it seems to be stable for the use cases it covers. It is worth noting though that binary content types do not work currently. |
@ProofOfKeags Thanks for the info Keagan. Do you mean binary content types, as in file downloads aren't working? |
I would be happy to test if you are still looking for testers. My application relies heavily on our JSON Api as well as file downloads. What is the best way to integrate this branch with my existing ionic/capacitor app for testing? I am currently on the latest for both ionic and capacitor. |
@mlynch going to plug this in to our enterprise app that we are rebuilding from the ground up and provide feedback on anything we run into. Any update on setting cookies automatically? |
@sta55en I'm referring to specifically "application/octet-stream". It is possible to hack the plugin to make it work but it would require some sort of utf8 expansion (b64/b16) in order to be able to marshal it back over the native <> js boundary |
let task = URLSession.shared.downloadTask(with: url) { (downloadLocation, response, error) in | ||
if error != nil { | ||
CAPLog.print("Error on download file", downloadLocation, response, error) | ||
call.reject("Error", error, [:]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing similar problems in PluginCall.java
. Probably just upstream changes that broke some things in your branch.
The current Android cookie implementation does not persist cookies when quitting the app. According to the |
Thanks everyone for the feedback. Took a little break from this and we agreed we're going to make this one available as our first package outside of the core APIs to make it easier to test and because we're interested in moving in that direction (this plugin would still be core but would be installed separately). I will look to incorporate some of the above modifications and update everyone here once that plugin is ready to test (which should be much easier w/ these changes) |
Alright the new version is up on Someone want to try it out? npm install @capacitor/http@next Make sure your Moving development to the new repo here: https://github.com/ionic-team/capacitor-plugin-http |
@sbannigan Thanks. I'm wondering if I should use the |
@sbannigan Alright I moved to the |
It possible to run capacitor plugins with Electron ? |
@mlynch I've installed |
@nbeekman if you call the |
@mlynch I've just tested it and it works fine if I set a cookie with setCookie and then get it with getCookies ! |
@zerock54 so you still have to manually set/getCookie with the plugin? |
Well it sounds like people would expect the cookie to actually get set in the webview as well, correct? It likely doesn't do that yet |
I am looking forward for this. I am suffering using HttpClient, fetch and cordova advanced http. What about the session cookie? Do I have to call it manually or it will be attached with each request after I login in? |
We are currently trying to make SSL pinning work but because that does not work with Angular's httpClient, I was looking for a native plugin and (yay!) someone is working on a Capacitor equivalent of So will the WIP also include enabling SSL pinning? See; https://github.com/silkimen/cordova-plugin-advanced-http#setservertrustmode |
Hey all, just an update: this plugin has moved to the (new) Capacitor Community org. More info about this org coming soon, but for now the plugin can be found here: https://github.com/capacitor-community/http |
Hi everyone, just published the first non-prerelease of And if you want to start using this plugin right now, you can install it from npm: https://www.npmjs.com/package/@capacitor-community/http Thanks so much to everyone for the help and feedback during this process! |
@mlynch : thanks so much for your work on this. One suggestion: I cannot, for any reason, think why this wouldn't be built into core. What's the advantage of making this a community plugin? My take is that this should be built into Capacitor and very much core functionality. There's a big opportunity for Capacitor to have "first class" HTTP support with cookies/sans-CORS. This is just my opinion, and it's heavily influenced by the "batteries included" philosophy of Python/Django, that I find sorely lacking in the NodeJS world. Just install Capacitor, and get to work with everything you (reasonably) need. Thanks again for putting it together. |
This is a work-in-progress attempt to build a native HTTP plugin for Capacitor to help developers avoid CORS issues.
The API is loosely modeled off of
fetch()
and axios, and has one core request method along with several cookie methods:Currently have basic JSON fetching and posting working on iOS and setting/getting cookies for URLs.
Tasks: