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

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

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


当前回答

我不确定是否有人愿意花这么大的力气来测试GET和POST调用。我使用Python Flask模块并编写了一个函数,该函数的功能类似于@Robert共享的功能。

from flask import Flask, request
app = Flask(__name__)

@app.route('/method', methods=['GET', 'POST'])
@app.route('/method/<wish>', methods=['GET', 'POST'])
def method_used(wish=None):
    if request.method == 'GET':
        if wish:
            if wish in dir(request):
                ans = None
                s = "ans = str(request.%s)" % wish
                exec s
                return ans
            else:
                return 'This wish is not available. The following are the available wishes: %s' % [method for method in dir(request) if '_' not in method]
        else:
            return 'This is just a GET method'
    else:
        return "You are using POST"

当我运行这个时,如下所示:

C:\Python27\python.exe E:/Arindam/Projects/Flask_Practice/first.py
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 581-155-269
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

现在让我们打几通电话。我正在使用浏览器。

http://127.0.0.1:5000/method This is just a GET method http://127.0.0.1:5000/method/NotCorrect This wish is not available. The following are the available wishes: ['application', 'args', 'authorization', 'blueprint', 'charset', 'close', 'cookies', 'data', 'date', 'endpoint', 'environ', 'files', 'form', 'headers', 'host', 'json', 'method', 'mimetype', 'module', 'path', 'pragma', 'range', 'referrer', 'scheme', 'shallow', 'stream', 'url', 'values'] http://127.0.0.1:5000/method/environ {'wsgi.multiprocess': False, 'HTTP_COOKIE': 'csrftoken=YFKYYZl3DtqEJJBwUlap28bLG1T4Cyuq', 'SERVER_SOFTWARE': 'Werkzeug/0.12.2', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/method/environ', 'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': '', 'werkzeug.server.shutdown': , 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': '127.0.0.1', 'REMOTE_PORT': 49569, 'wsgi.url_scheme': 'http', 'SERVER_PORT': '5000', 'werkzeug.request': , 'wsgi.input': , 'HTTP_HOST': '127.0.0.1:5000', 'wsgi.multithread': False, 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'HTTP_ACCEPT': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 'wsgi.version': (1, 0), 'wsgi.run_once': False, 'wsgi.errors': ", mode 'w' at 0x0000000002042150>", 'REMOTE_ADDR': '127.0.0.1', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch, br'}

其他回答

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

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

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

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

你可能不需要任何网站,只需要打开浏览器,按F12来访问开发人员工具>控制台,然后在控制台写一些JavaScript代码来做到这一点。

下面我将分享一些实现这一目标的方法:

对于GET请求: *。使用jQuery:

$.get("http://someurl/status/?messageid=597574445", function(data, status){
      console.log(data, status);
});

POST请求:

使用jQuery $.ajax:

var url= "http://someurl/",
          api_key = "6136-bc16-49fb-bacb-802358",
          token1 = "Just for test",
          result;
    $.ajax({
            url: url,
            type: "POST",
            data: {
              api_key: api_key,
              token1: token1
            },
          }).done(function(result) {
                  console.log("done successfuly", result);
          }).fail(function(error) {
              console.log(error.responseText, error);
          });

使用jQuery,添加和提交

var merchantId = "AA86E",
    token = "4107120133142729",
    url = "https://payment.com/Index";

var form = `<form id="send-by-post" method="post" action="${url}">
            <input id="token" type="hidden" name="token" value="${merchantId}"/>
            <input id="merchantId" name="merchantId" type="hidden" value="${token}"/>
            <button type="submit" >Pay</button>
            </div>
            </form> `; 
    $('body').append(form);
    $("#send-by-post").submit();//Or $(form).appendTo("body").submit();

使用纯JavaScript:

`var api_key = "73736-bc16-49fb-bacb-643e58",
    recipient = "095552565",
    token1 = "4458",
    url = 'http://smspanel.com/send/';`

``var form = `<form id="send-by-post" method="post" action="${url}">
              <input id="api_key" type="hidden" name="api_key" value="${api_key}"/>
              <input id="recipient" type="hidden" name="recipient"  value="${recipient}"/>
              <input id="token1" name="token1" type="hidden" value="${token1}"/>
              <button type="submit" >Send</button>
        </div>
    </form>`;``

document.querySelector("body").insertAdjacentHTML('beforeend',form);
document.querySelector("#send-by-post").submit();

甚至使用ASP。Net:

var url = "https://Payment.com/index";
Response.Clear();
var sb = new System.Text.StringBuilder();

sb.Append("<html>");
sb.AppendFormat("<body onload='document.forms[0].submit()'>");
sb.AppendFormat("<form action='{0}' method='post'>", url);
sb.AppendFormat("<input type='hidden' name='merchantId' value='{0}'>", "C668");
sb.AppendFormat("<input type='hidden' name='Token' value='{0}'>", "22720281459");
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");
Response.Write(sb.ToString());
Response.End();

另一个提供一些定制且易于使用(无需安装、注册)的网站是https://beeceptor.com。

您创建一个端点,向它发出初始请求,并可以调整响应。

下面是一个邮差回声:https://docs.postman-echo.com/

例子:

curl --request POST \
  --url https://postman-echo.com/post \
  --data 'This is expected to be sent back as part of response body.'

回应:

{"args":{},"data":"","files":{},"form":{"This is expected to be sent back as part of response body.":""},"headers":{"host":"postman-echo.com","content-length":"58","accept":"*/*","content-type":"application/x-www-form-urlencoded","user-agent":"curl/7.54.0","x-forwarded-port":"443","x-forwarded-proto":"https"},"json":{"...

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.