如何向 google 地图 api 发送 http 请求
How to send http request to google map api
这是我在 node.js 应用程序中提出的请求:
var https = require('https');
var options = {
host: 'maps.googleapis.com',
path: '/maps/api/geocode/json',
method: 'GET',
useQuerystring: true,
qs: 'address=' + "1600+Amphitheatre+Parkway,+Mountain+View,+CA" + "&key=[redacted in stack overflow post]"
};
var req = https.request(options, function(response) {
console.log(response);
//Uncomment the code below when you start getting valid responses
//response.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
//});
});
console.log(req);
req.end();
但是响应返回 statusCode:400,statusMessage:'Bad Request'。完整的回复可以在下面找到。
我在请求或响应中都没有看到我的查询字符串,这让我很担心。我试过从选项中删除 useQuerystring: true,
,这似乎没有效果。我也试过添加 www。在 maps.googleapis.com 前面,我知道这是行不通的,并确认了这一点。
我正在向 Postman 发出成功的请求,如下所示:
GET https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=[redacted in stack overflow post]
如何使用 https 模块在 Node 中发出能获得成功响应的请求?
这是 console.log(req)
的输出:
ClientRequest {
domain: null,
_events:
{ response: { [Function: g] listener: [Function] },
socket: { [Function: g] listener: [Function: onSocket] } },
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: false,
socket: null,
connection: null,
_header: null,
_headers: { host: 'maps.googleapis.com' },
_headerNames: { host: 'Host' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: { 'maps.googleapis.com:443::::::::': [Object] },
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false }
这是 console.log(res)
的输出:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: true,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: { end: [Function: responseOnEnd] },
_eventsCount: 1,
_maxListeners: undefined,
socket:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: false,
headers:
{ 'content-type': 'application/json; charset=UTF-8',
date: 'Sat, 20 May 2017 15:12:32 GMT',
pragma: 'no-cache',
expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
'cache-control': 'no-cache, must-revalidate',
'access-control-allow-origin': '*',
server: 'mafe',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'quic=":443"; ma=2592000; v="37,36,35"',
'accept-ranges': 'none',
vary: 'Accept-Encoding',
connection: 'close' },
rawHeaders:
[ 'Content-Type',
'application/json; charset=UTF-8',
'Date',
'Sat, 20 May 2017 15:12:32 GMT',
'Pragma',
'no-cache',
'Expires',
'Fri, 01 Jan 1990 00:00:00 GMT',
'Cache-Control',
'no-cache, must-revalidate',
'Access-Control-Allow-Origin',
'*',
'Server',
'mafe',
'X-XSS-Protection',
'1; mode=block',
'X-Frame-Options',
'SAMEORIGIN',
'Alt-Svc',
'quic=":443"; ma=2592000; v="37,36,35"',
'Accept-Ranges',
'none',
'Vary',
'Accept-Encoding',
'Connection',
'close' ],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 400,
statusMessage: 'Bad Request',
client:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
_consuming: false,
_dumped: false,
req:
ClientRequest {
domain: null,
_events: { prefinish: [Function: requestOnPrefinish] },
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'maps.googleapis.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'maps.googleapis.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: { host: 'maps.googleapis.com' },
_headerNames: { host: 'Host' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Object],
incoming: [Circular],
outgoing: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
res: [Circular] } }
注意:我已阅读 ,这不是我的问题。我特别不应该包括 www。在 maps.googleapis.com
前面
对于那些稍后阅读这篇文章的人,一旦你收到了很好的回应,你可能想阅读这个:
您可以尝试在 URL 本身中指定 queryString:path:'/maps/api/geocode/json' + '?' + <QueryString>
或者,您可以考虑使用 'requestify' 模块。它可以通过'npm'安装。
源代码可在以下位置找到:-
https://github.com/ranm8/requestify
因此,您的代码将类似于:-
var requestify = require('requestify');
requestify.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=<API_KEY>').then(function(response) {
// Get the response body
response.getBody();
});
IMP: 记得在URL中替换你自己的API_KEY没有angular括号。
希望这对您有所帮助..:)
这是我在 node.js 应用程序中提出的请求:
var https = require('https');
var options = {
host: 'maps.googleapis.com',
path: '/maps/api/geocode/json',
method: 'GET',
useQuerystring: true,
qs: 'address=' + "1600+Amphitheatre+Parkway,+Mountain+View,+CA" + "&key=[redacted in stack overflow post]"
};
var req = https.request(options, function(response) {
console.log(response);
//Uncomment the code below when you start getting valid responses
//response.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
//});
});
console.log(req);
req.end();
但是响应返回 statusCode:400,statusMessage:'Bad Request'。完整的回复可以在下面找到。
我在请求或响应中都没有看到我的查询字符串,这让我很担心。我试过从选项中删除 useQuerystring: true,
,这似乎没有效果。我也试过添加 www。在 maps.googleapis.com 前面,我知道这是行不通的,并确认了这一点。
我正在向 Postman 发出成功的请求,如下所示:
GET https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=[redacted in stack overflow post]
如何使用 https 模块在 Node 中发出能获得成功响应的请求?
这是 console.log(req)
的输出:
ClientRequest {
domain: null,
_events:
{ response: { [Function: g] listener: [Function] },
socket: { [Function: g] listener: [Function: onSocket] } },
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: false,
socket: null,
connection: null,
_header: null,
_headers: { host: 'maps.googleapis.com' },
_headerNames: { host: 'Host' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: { 'maps.googleapis.com:443::::::::': [Object] },
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false }
这是 console.log(res)
的输出:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: true,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: { end: [Function: responseOnEnd] },
_eventsCount: 1,
_maxListeners: undefined,
socket:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: false,
headers:
{ 'content-type': 'application/json; charset=UTF-8',
date: 'Sat, 20 May 2017 15:12:32 GMT',
pragma: 'no-cache',
expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
'cache-control': 'no-cache, must-revalidate',
'access-control-allow-origin': '*',
server: 'mafe',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'quic=":443"; ma=2592000; v="37,36,35"',
'accept-ranges': 'none',
vary: 'Accept-Encoding',
connection: 'close' },
rawHeaders:
[ 'Content-Type',
'application/json; charset=UTF-8',
'Date',
'Sat, 20 May 2017 15:12:32 GMT',
'Pragma',
'no-cache',
'Expires',
'Fri, 01 Jan 1990 00:00:00 GMT',
'Cache-Control',
'no-cache, must-revalidate',
'Access-Control-Allow-Origin',
'*',
'Server',
'mafe',
'X-XSS-Protection',
'1; mode=block',
'X-Frame-Options',
'SAMEORIGIN',
'Alt-Svc',
'quic=":443"; ma=2592000; v="37,36,35"',
'Accept-Ranges',
'none',
'Vary',
'Accept-Encoding',
'Connection',
'close' ],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 400,
statusMessage: 'Bad Request',
client:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'maps.googleapis.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 608,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
_consuming: false,
_dumped: false,
req:
ClientRequest {
domain: null,
_events: { prefinish: [Function: requestOnPrefinish] },
_eventsCount: 1,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: {},
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'maps.googleapis.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'maps.googleapis.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 85,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
_header: 'GET /maps/api/geocode/json HTTP/1.1\r\nHost: maps.googleapis.com\r\nConnection: close\r\n\r\n',
_headers: { host: 'maps.googleapis.com' },
_headerNames: { host: 'Host' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/maps/api/geocode/json',
_ended: false,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Object],
incoming: [Circular],
outgoing: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
res: [Circular] } }
注意:我已阅读 ,这不是我的问题。我特别不应该包括 www。在 maps.googleapis.com
前面对于那些稍后阅读这篇文章的人,一旦你收到了很好的回应,你可能想阅读这个:
您可以尝试在 URL 本身中指定 queryString:path:'/maps/api/geocode/json' + '?' + <QueryString>
或者,您可以考虑使用 'requestify' 模块。它可以通过'npm'安装。
源代码可在以下位置找到:- https://github.com/ranm8/requestify
因此,您的代码将类似于:-
var requestify = require('requestify');
requestify.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=<API_KEY>').then(function(response) {
// Get the response body
response.getBody();
});
IMP: 记得在URL中替换你自己的API_KEY没有angular括号。
希望这对您有所帮助..:)