zyra / cordova-plugin-native-http

[WIP] Native HTTP Client plugin for Cordova Apps
3 stars 2 forks source link

Complete? #1

Open arjunmenon opened 6 years ago

arjunmenon commented 6 years ago

Hey Is this project complete? Would like to use it instead of what cordova webview allows. Cordova doesn't allow connections unless it is defined in the config.xml

Also, the Android's okhttp library allows custom http requests. No need to specifically set a methods list. Some servers like Upnp asks it to connect by SUBSCRIBE and NOTIFY.

Would be nice if the lib allows it to set other HTTP methods, even for the ios versions

ihadeed commented 6 years ago

Hey

It's still work in progress..

Basic functionality should work fine (get/post) .. other things like SSL pinning or download/upload will not work.

arjunmenon commented 6 years ago

How do you build headers though here? This is what I used to send using XHR

var payload3 = `<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <s:Body>
  <u:GetTransportInfo xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
    <InstanceID>0</InstanceID>
  </u:GetTransportInfo>
 </s:Body>
</s:Envelope>`;

var xmlHTTP2 = new XMLHttpRequest();

xmlHTTP2.open('POST', 'http://192.168.1.6:49152/ctl/AVTransport', true);

xmlHTTP2.setRequestHeader('SOAPAction', '"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo"');
xmlHTTP2.setRequestHeader('Content-Type', 'text/xml; charset="utf-8"');
xmlHTTP2.onreadystatechange = function() {
  if(xmlHTTP2.readyState === xmlHTTP2.DONE) {

        console.log(xmlHTTP2.responseText);
    } else {
        console.log("not OK: " + JSON.stringify(xmlHTTP2));

  }
};

xmlHTTP2.send(payload3);

Payload is actually a command substitution, notice the backtick

ihadeed commented 6 years ago

See the method signatures here: https://github.com/zyra/cordova-plugin-native-http/blob/master/www/NativeHttp.ts#L190-L206

The third param is headers for all of them, you can pass an object there.

const payload3 = `<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <s:Body>
  <u:GetTransportInfo xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
    <InstanceID>0</InstanceID>
  </u:GetTransportInfo>
 </s:Body>
</s:Envelope>`;

const path = 'http://192.168.1.6:49152/ctl/AVTransport';

const headers = {
  SOAPAction: '"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo"',
  'Content-Type': 'text/xml; charset="utf-8"'
};

NativeHttp.post(path, {}, headers, payload3, false);