| 1 | // Example using HTTP POST operation |
| 2 | |
| 3 | "use strict"; |
| 4 | var page = require('webpage').create(), |
| 5 | server = require('webserver').create(), |
| 6 | system = require('system'), |
| 7 | data = 'universe=expanding&answer=42'; |
| 8 | |
| 9 | if (system.args.length !== 2) { |
| 10 | console.log('Usage: postserver.js <portnumber>'); |
| 11 | phantom.exit(1); |
| 12 | } |
| 13 | |
| 14 | var port = system.args[1]; |
| 15 | |
| 16 | service = server.listen(port, function (request, response) { |
| 17 | console.log('Request received at ' + new Date()); |
| 18 | |
| 19 | response.statusCode = 200; |
| 20 | response.headers = { |
| 21 | 'Cache': 'no-cache', |
| 22 | 'Content-Type': 'text/plain;charset=utf-8' |
| 23 | }; |
| 24 | response.write(JSON.stringify(request, null, 4)); |
| 25 | response.close(); |
| 26 | }); |
| 27 | |
| 28 | page.open('http://localhost:' + port + '/', 'post', data, function (status) { |
| 29 | if (status !== 'success') { |
| 30 | console.log('Unable to post!'); |
| 31 | } else { |
| 32 | console.log(page.plainText); |
| 33 | } |
| 34 | phantom.exit(); |
| 35 | }); |