Skip to content

HTTP networking plugin that brings the fetch() standard to Cordova

Notifications You must be signed in to change notification settings

leoleitemelo/cordova-plugin-fetch

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cordova-plugin-fetch

HTTP networking plugin that brings the whatwg fetch spec standard to Cordova

Features

  • Consistent with the window.fetch API based on the whatwg fetch spec
  • Supports iOS and Android
  • Allows cross origin requests and ignores content security policy
  • Allows all type of headers (including access to Set-Cookie and User-Agent)

License

MIT

Installation

The plugin conforms to the Cordova plugin specification, it can be installed using the Cordova / Phonegap command line interface.

cordova plugin add https://github.com/aporat/cordova-plugin-fetch.git

Usage

The cordovaFetch function supports any HTTP method. We'll focus on GET and POST example requests.

HTML

cordovaFetch('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

JSON

cordovaFetch('/users.json')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })

Setting Custom User Agent

cordovaFetch('/users.json', {
  method : 'GET',
  headers: {
    'User-Agent': 'CordovaFetch 1.0.0'
  },
})

Accessing Response Headers / Cookies

cordovaFetch('/users.json')
.then(function(response) {
  console.log(res.headers['Set-Cookie']);
})

Post form

var form = document.querySelector('form')

cordovaFetch('/users', {
  method: 'POST',
  body: new FormData(form)
})

Post JSON

cordovaFetch('/users.json', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'Hubot',
    login: 'hubot',
  })
})

About

HTTP networking plugin that brings the fetch() standard to Cordova

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 94.2%
  • JavaScript 2.9%
  • Java 2.2%
  • C 0.7%