Added JSON and Encoding examples

POST json data to your.custom.api in utf-8 encoding
gh-pages
Gal Schlezinger 2014-10-04 12:56:52 +03:00
parent f5fdb68a93
commit 0fe8544863
1 changed files with 23 additions and 1 deletions

View File

@ -8,6 +8,7 @@ permalink: api/webpage/method/open.html
`open(url, callback)` {void}
`open(url, method, callback)` {void}
`open(url, method, data, callback)` {void}
`open(url, settings, callback)` {void}
Opens the `url` and loads it to the page. Once the page is loaded, the optional `callback` is called using `page.onLoadFinished`, and also provides the page status to the function (`'success'` or `'fail'`).
@ -40,7 +41,28 @@ page.open('http://www.google.com/', 'POST', postBody, function(status) {
});
```
### POST json data to your.custom.api in utf-8 encoding
As of PhantomJS 1.9, the open function can get an object of settings. and with a use of "encoding" key, you can set the custom encoding to your app.
In this example, we've set the encoding to `UTF8`, and set the `Content-Type` header to `application/json` for making our server know the request has information in `json` format and not in `urlencoded` format.
```javascript
var webPage = require('webpage');
var page = webPage.create();
var data = {
some: "data",
another: ["custom", "data"]
};
page.open('http://your.custom.api', {
data: data,
encoding: "utf8",
headers: {
"Content-Type": "application/json"
}
}, function(status) {
console.log('Status: ' + status);
// Do other things here...
});