我需要一个实时测试服务器,它通过HTTP GET接受我对基本信息的请求,并允许我POST(即使它真的什么都不做)。这完全是为了测试目的。

这里有一个很好的例子。它很容易接受GET请求,但我需要一个接受POST请求以及。

有人知道我也可以发送虚拟测试消息的服务器吗?


当前回答

如果你想要一个本地测试服务器接受任何URL并将请求转储到控制台,你可以使用node:

const http = require("http");

const hostname = "0.0.0.0";
const port = 3000;

const server = http.createServer((req, res) => {
  console.log(`\n${req.method} ${req.url}`);
  console.log(req.headers);

  req.on("data", function(chunk) {
    console.log("BODY: " + chunk);
  });

  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World\n");
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

将其保存在文件'echo.js'中,并按如下方式运行:

$ node echo.js
Server running at http://localhost:3000/

然后你可以提交数据:

$ curl -d "[1,2,3]" -XPOST http://localhost:3000/foo/bar

这将显示在服务器的标准输出:

POST /foo/bar
{ host: 'localhost:3000',
  'user-agent': 'curl/7.54.1',
  accept: '*/*',
  'content-length': '7',
  'content-type': 'application/x-www-form-urlencoded' }
BODY: [1,2,3]

其他回答

如果你需要或想要一个简单的HTTP服务器与以下:

是否可以在本地运行或在与公共Internet密封的网络中运行 有基本的认证吗 处理POST请求

我在PyPI上已有的出色的SimpleHTTPAuthServer之上构建了一个。这增加了POST请求的处理: https://github.com/arielampol/SimpleHTTPAuthServerWithPOST

否则,所有其他公开可用的选项都已经很好很健壮了。

https://httpbin.org/

它将在请求中使用以下类型的任何数据进行回显:

https://httpbin.org/anything Returns most of the below. https://httpbin.org/ip Returns Origin IP. https://httpbin.org/user-agent Returns user-agent. https://httpbin.org/headers Returns header dict. https://httpbin.org/get Returns GET data. https://httpbin.org/post Returns POST data. https://httpbin.org/put Returns PUT data. https://httpbin.org/delete Returns DELETE data https://httpbin.org/gzip Returns gzip-encoded data. https://httpbin.org/status/:code Returns given HTTP Status code. https://httpbin.org/response-headers?key=val Returns given response headers. https://httpbin.org/redirect/:n 302 Redirects n times. https://httpbin.org/relative-redirect/:n 302 Relative redirects n times. https://httpbin.org/cookies Returns cookie data. https://httpbin.org/cookies/set/:name/:value Sets a simple cookie. https://httpbin.org/basic-auth/:user/:passwd Challenges HTTPBasic Auth. https://httpbin.org/hidden-basic-auth/:user/:passwd 404'd BasicAuth. https://httpbin.org/digest-auth/:qop/:user/:passwd Challenges HTTP Digest Auth. https://httpbin.org/stream/:n Streams n–100 lines. https://httpbin.org/delay/:n Delays responding for n–10 seconds.

看看PutsReq,它与其他类似,但它也允许您使用JavaScript编写您想要的响应。

http://requestb.in类似于前面提到的工具,也有一个非常漂亮的UI。

RequestBin为您提供了一个URL,该URL将收集向它发出的请求,并让您以一种人性化的方式检查它们。 使用RequestBin查看HTTP客户端发送的内容或检查和调试webhook请求。

虽然它已于2018年3月21日停产。

由于持续的滥用,我们已经停止了RequestBin的公开托管版本,这使得它很难保持站点的可靠运行。请参阅有关设置自己的自托管实例的说明。

我不知道为什么这里所有的答案都让一个很简单的工作很辛苦!

当HTTP上有一个请求时,实际上客户端会发送一个HTTP_MESSAGE到服务器(阅读HTTP_MESSAGE是什么),你可以用2个简单的步骤创建一个服务器:

安装netcat: 在许多基于unix的系统中,你已经安装了这个,如果你有windows只是谷歌它,安装过程真的很简单,你只需要一个nc.exe文件,然后你应该复制这个nc.exe文件的路径到你的path环境变量,并检查nc -h是否一切正常 创建一个监听localhost:12345的服务器: 只要在你的终端上输入nc -l -p 12345,一切就完成了!(在mac nc -l 12345 tnx Silvio Biasiol)


现在你有一个服务器在http://localhost:12345上监听,例如,如果你是一个js开发人员,你可以用axios发出post请求:

axios.post('http://localhost:12345', { firstName: 'Fred' })

或者制作自己的xhr,或者在HTML文件中制作表单并将其提交给服务器,例如:

<form action="http://localhost:12345" method="post">

或者使用curl或wget等进行请求。然后检查你的终端,一个原始的HTTP_MESSAGE应该出现在你的终端上,你可以开始你的快乐黑客;)