Created
September 15, 2017 05:11
-
-
Save Kxrr/3a6ddb7a4b9989e463704e1160cf2b83 to your computer and use it in GitHub Desktop.
Puppeteer's response.buffer() issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const puppeteer = require('puppeteer'); | |
const ORIGINAL_DATA = Buffer.from(JSON.stringify({'msg': '测试'})); | |
function serve() { | |
http.createServer(function (req, res) { | |
// The correct header should be 'application/json; charset=utf-8' | |
// but I change it to 'text/plain' to REPRODUCE THE BUG | |
res.setHeader('Content-Type', 'text/plain'); | |
res.writeHead(200); | |
res.end(ORIGINAL_DATA); | |
}).listen(8001); | |
} | |
async function responseSuccess(res) { | |
let dataReceived = await res.buffer(); | |
console.log('Received:\n', dataReceived); | |
console.log('Original:\n', ORIGINAL_DATA); | |
console.log('\n') | |
} | |
async function doRequest() { | |
const browser = await puppeteer.launch({headless: false}); | |
const page = await browser.newPage(); | |
page.on('response', responseSuccess); | |
let res = await page.goto('http://localhost:8001'); | |
} | |
serve(); | |
doRequest(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment