我的GET请求有什么问题?

时间:2015-09-04 16:42:56

标签: http lua httprequest http-get nodemcu

很抱歉打扰一些应该很容易的事情。

我有这个HTTP GET请求:

GET /ip HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

当我通过ESP8266发送此请求时,它会返回404错误:

HTTP/1.1 404 Not Found
Date: Fri, 04 Sep 2015 16:34:46 GMT
Server: Apache
Content-Length: 1363
X-Frame-Options: deny
Connection: close
Content-Type: text/html

但是,当我(和你)去http://httpbin.org/ip时,它完美无缺!

有什么问题?

详情

我在Lua中构建我的请求:

conn:on("connection", function(conn, payload)
    print('\nConnected')
    req = "GET /ip"
    .." HTTP/1.1\r\n"
    .."Host: httpbin.org\r\n"
    .."Connection: close\r\n"
    .."Accept: */*\r\n"
    .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
    .."\r\n"
    print(req)
    conn:send(req)
end)

如果我使用其他主机(given is this example),它可以工作:

conn:on("connection", function(conn, payload)
    print('\nConnected')
    conn:send("GET /esp8266/test.php?"
    .."T="..(tmr.now()-Tstart)
    .."&heap="..node.heap()
    .." HTTP/1.1\r\n"
    .."Host: benlo.com\r\n"
    .."Connection: close\r\n"
    .."Accept: */*\r\n"
    .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
    .."\r\n")
end)

3 个答案:

答案 0 :(得分:2)

您实际连接到httpbin.org吗?或者在其他地方?

我刚尝试通过在telnet中输入您的请求来发出请求,这对我有用。但服务器响应是nginx,而您的示例显示apache。

$ telnet httpbin.org 80
Trying 54.175.219.8...
Connected to httpbin.org.
Escape character is '^]'.
GET /ip HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 07 Oct 2015 06:08:40 GMT
Content-Type: application/json
Content-Length: 32
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "origin": "124.149.55.34"
}
Connection closed by foreign host.

当我尝试使用其他URI来强制执行404响应时,我看到了这一点:

HTTP/1.1 404 NOT FOUND
Server: nginx
Date: Wed, 07 Oct 2015 06:12:21 GMT
Content-Type: text/html
Content-Length: 233
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

这又与你在httpbin.org上提到的回复完全不同。

答案 1 :(得分:1)

http.get("http://httpbin.org/ip", nil, function(code, data)
   if (code < 0) then
      print("HTTP request failed")
   else
      print(code, data)
   end
end)

http.post('http://httpbin.org/post',
    'Content-Type: application/json\r\n',
    '{"hello":"world"}',
    function(code, data)
        if (code < 0) then
          print("HTTP request failed")
       else
          print(code, data)
       end
     end)

参见参考here

答案 2 :(得分:0)

这是您的服务器不喜欢的请求行。 这将完成这项工作:

GET http://httpbin.org/ip HTTP/1.1
Host: httpbin.org