标签:class mem head type parse content request try erro
const http = require(‘http‘); http.get(‘http://192.168.1.6:8080/getDemo?msg=12‘, (res) => { const { statusCode } = res; const contentType = res.headers[‘content-type‘]; let error; if (statusCode !== 200) { error = new Error(`Request Failed.\n` + `Status Code: ${statusCode}`); } else if (!/^application\/json/.test(contentType)) { error = new Error(`Invalid content-type.\n` + `Expected application/json but received ${contentType}`); } if (error) { console.error(error.message); // consume response data to free up memory res.resume(); return; } res.setEncoding(‘utf8‘); let rawData = ‘‘; res.on(‘data‘, (chunk) => { rawData += chunk; }); res.on(‘end‘, () => { try { const parsedData = JSON.parse(rawData); console.log(parsedData); } catch (e) { console.error(e.message); } }); }).on(‘error‘, (e) => { console.error(`Got error: ${e.message}`); });
标签:class mem head type parse content request try erro
原文地址:http://www.cnblogs.com/thankyouGod/p/6842079.html