From 0fe8544863f0ca23fc7201dcc1769a85d29fc2a8 Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Sat, 4 Oct 2014 12:56:52 +0300 Subject: [PATCH] Added JSON and Encoding examples POST json data to your.custom.api in utf-8 encoding --- _posts/api/webpage/method/2000-01-01-open.md | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/_posts/api/webpage/method/2000-01-01-open.md b/_posts/api/webpage/method/2000-01-01-open.md index 84492a7a..c9a73e2d 100644 --- a/_posts/api/webpage/method/2000-01-01-open.md +++ b/_posts/api/webpage/method/2000-01-01-open.md @@ -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... +});