Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 4 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. cordovaHTTP
  2. ==================
  3. Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
  4. ## Advantages over Javascript requests
  5. - Background threading - all requests are done in a background thread.
  6. - SSL Pinning - read more at [LumberBlog](http://blog.lumberlabs.com/2012/04/why-app-developers-should-care-about.html).
  7. ## Updates
  8. Please check [CHANGELOG.md](CHANGELOG.md) for details about updating to a new version.
  9. ## Installation
  10. The plugin conforms to the Cordova plugin specification, it can be installed
  11. using the Cordova / Phonegap command line interface.
  12. phonegap plugin add cordova-plugin-http
  13. cordova plugin add cordova-plugin-http
  14. ## Usage
  15. ### AngularJS
  16. This plugin creates a cordovaHTTP service inside of a cordovaHTTP module. You must load the module when you create your app's module.
  17. var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'cordovaHTTP']);
  18. You can then inject the cordovaHTTP service into your controllers. The functions can then be used identically to the examples shown below except that instead of accepting success and failure callback functions, each function returns a promise. For more information on promises in AngularJS read the [AngularJS docs](http://docs.angularjs.org/api/ng/service/$q). For more info on promises in general check out this article on [html5rocks](http://www.html5rocks.com/en/tutorials/es6/promises/). Make sure that you load cordova.js or phonegap.js after AngularJS is loaded.
  19. ### Not AngularJS
  20. This plugin registers a `cordovaHTTP` global on window
  21. ## Sync Functions
  22. ### getBasicAuthHeader
  23. This returns an object representing a basic HTTP Authorization header of the form `{'Authorization': 'Basic base64encodedusernameandpassword'}`
  24. var header = cordovaHTTP.getBasicAuthHeader("user", "password");
  25. ### useBasicAuth
  26. This sets up all future requests to use Basic HTTP authentication with the given username and password.
  27. cordovaHTTP.useBasicAuth("user", "password");
  28. ### setHeader
  29. Set a header for all future requests. Takes a header and a value.
  30. cordovaHTTP.setHeader("Header", "Value");
  31. ## Async Functions
  32. These functions all take success and error callbacks as their last 2 arguments.
  33. ### enableSSLPinning
  34. Enable or disable SSL pinning. This defaults to false.
  35. To use SSL pinning you must include at least one .cer SSL certificate in your app project. You can pin to your server certificate or to one of the issuing CA certificates. For ios include your certificate in the root level of your bundle (just add the .cer file to your project/target at the root level). For android include your certificate in your project's platforms/android/assets folder. In both cases all .cer files found will be loaded automatically. If you only have a .pem certificate see this [stackoverflow answer](http://stackoverflow.com/a/16583429/3182729). You want to convert it to a DER encoded certificate with a .cer extension.
  36. As an alternative, you can store your .cer files in the www/certificates folder.
  37. cordovaHTTP.enableSSLPinning(true, function() {
  38. console.log('success!');
  39. }, function() {
  40. console.log('error :(');
  41. });
  42. ### acceptAllCerts
  43. Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.
  44. cordovaHTTP.acceptAllCerts(true, function() {
  45. console.log('success!');
  46. }, function() {
  47. console.log('error :(');
  48. });
  49. ### validateDomainName
  50. Whether or not to validate the domain name in the certificate. This defaults to true.
  51. cordovaHTTP.validateDomainName(false, function() {
  52. console.log('success!');
  53. }, function() {
  54. console.log('error :(');
  55. });
  56. ### post<a name="post"></a>
  57. Execute a POST request. Takes a URL, parameters, and headers.
  58. #### success
  59. The success function receives a response object with 3 properties: status, data, and headers. Status is the HTTP response code. Data is the response from the server as a string. Headers is an object with the headers. Here's a quick example:
  60. {
  61. status: 200,
  62. data: "{'id': 12, 'message': 'test'}",
  63. headers: {
  64. "Content-Length": "247"
  65. }
  66. }
  67. Most apis will return JSON meaning you'll want to parse the data like in the example below:
  68. cordovaHTTP.post("https://google.com/", {
  69. id: 12,
  70. message: "test"
  71. }, { Authorization: "OAuth2: token" }, function(response) {
  72. // prints 200
  73. console.log(response.status);
  74. try {
  75. response.data = JSON.parse(response.data);
  76. // prints test
  77. console.log(response.data.message);
  78. } catch(e) {
  79. console.error("JSON parsing error");
  80. }
  81. }, function(response) {
  82. // prints 403
  83. console.log(response.status);
  84. //prints Permission denied
  85. console.log(response.error);
  86. });
  87. #### failure
  88. The error function receives a response object with 3 properties: status, error and headers. Status is the HTTP response code. Error is the error response from the server as a string. Headers is an object with the headers. Here's a quick example:
  89. {
  90. status: 403,
  91. error: "Permission denied",
  92. headers: {
  93. "Content-Length": "247"
  94. }
  95. }
  96. ### get
  97. Execute a GET request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
  98. cordovaHTTP.get("https://google.com/", {
  99. id: 12,
  100. message: "test"
  101. }, { Authorization: "OAuth2: token" }, function(response) {
  102. console.log(response.status);
  103. }, function(response) {
  104. console.error(response.error);
  105. });
  106. ### uploadFile
  107. Uploads a file saved on the device. Takes a URL, parameters, headers, filePath, and the name of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure.
  108. cordovaHTTP.uploadFile("https://google.com/", {
  109. id: 12,
  110. message: "test"
  111. }, { Authorization: "OAuth2: token" }, "file:///somepicture.jpg", "picture", function(response) {
  112. console.log(response.status);
  113. }, function(response) {
  114. console.error(response.error);
  115. });
  116. ### downloadFile
  117. Downloads a file and saves it to the device. Takes a URL, parameters, headers, and a filePath. See [post](#post) documentation for details on what is returned on failure. On success this function returns a cordova [FileEntry object](http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#FileEntry).
  118. cordovaHTTP.downloadFile("https://google.com/", {
  119. id: 12,
  120. message: "test"
  121. }, { Authorization: "OAuth2: token" }, "file:///somepicture.jpg", function(entry) {
  122. // prints the filename
  123. console.log(entry.name);
  124. // prints the filePath
  125. console.log(entry.fullPath);
  126. }, function(response) {
  127. console.error(response.error);
  128. });
  129. ## Libraries
  130. This plugin utilizes some awesome open source networking libraries. These are both MIT licensed:
  131. - iOS - [AFNetworking](https://github.com/AFNetworking/AFNetworking)
  132. - Android - [http-request](https://github.com/kevinsawicki/http-request)
  133. We made a few modifications to http-request. They can be found in a separate repo here: https://github.com/wymsee/http-request
  134. ## Cookies
  135. - a cookie set by a request isn't sent in subsequent requests
  136. Take this into account when using this plugin in your application.
  137. ## License
  138. The MIT License
  139. Copyright (c) 2014 Wymsee, Inc
  140. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  141. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  142. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.