首页 > 解决方案 > NodeJS 使用 absoluteURI 发送 http 请求

问题描述

我正在使用一个奇怪的 HTTP 服务器,它只接受 HTTP 请求,如下所示:

GET http://10.0.0.1/test
Host: 10.0.0.1
Cache-Control: no cache
Connection: Keep-Alive

在路径中添加“http://”节点

当我发送 HTTP 请求(使用底部的代码)时,请求如下所示:

GET /test
Host: 10.0.0.1
Cache-Control: no cache
Connection: Keep-Alive

代码示例:

var request = require('request');
var options = {
  'method': 'GET',
  'uri': 'http://10.0.0.1/test',
  'headers': {
    'Host': '10.0.0,1',
    'Cache-Control': 'no cache',
    'Connection': 'Keep-Alive'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

知道如何像服务器期望的那样使用标准库发送请求吗?

标签: node.js

解决方案


推荐阅读