【问题标题】:curl url with option location not working properly带有选项位置的 curl url 无法正常工作
【发布时间】:2021-05-18 15:26:39
【问题描述】:

我在 WebLogic 上部署了一个 Java Servlet 应用程序。客户端发出一个 POST 请求,例如 /WebServletApp/employee。 EmployeeServlet (doPost) 接收到它,并且对于某些特定条件,即并非总是如此,代码会进行重定向调用 (GET)。

resp.sendRedirect("http://"+host+"/WebServletApp/defaultEmployee?r="+r);

现在上面的重定向 URL /WebServletApp/defaultEmployee 由 DefaultEmployeeServlet (doGet) 提供服务。

如果我使用 Postman 客户端发送第一个 POST 请求。重定向之后,我从 DefaultEmployeeServlet 获得了预期的响应。

但是,如果我从终端使用 curl -L 发送请求,则不遵循重定向并且我收到错误

此 URL 不支持 HTTP 方法 POST

为什么会这样? '-L' 或 '--location' 应该通过向重定向给出的新位置 URI 发出 GET 请求来帮助 curl 自动跟随重定向,对吗?我在这里做错了什么?

为什么 Postman 工作而不卷曲?

这是命令输出。

curl -iL --request POST '127.0.0.1:7001/WebServletApp/employee' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'firstName=Harshit' \
--data-urlencode 'lastName=Rajput' \
--data-urlencode 'emailId=harajput@gmail.com' \
--data-urlencode 'password=yolo!'
HTTP/1.1 302 Moved Temporarily
Date: Tue, 18 May 2021 15:19:47 GMT
Location: http://127.0.0.1:7001/WebServletApp/defaultEmployee?r=18
Content-Length: 309
Content-Type: text/html
X-ORACLE-DMS-ECID: 176df710-a1ae-4554-9c30-5bf795293f7f-0000003a
X-ORACLE-DMS-RID: 0

HTTP/1.1 405 Method Not Allowed
Date: Tue, 18 May 2021 15:19:49 GMT
Content-Length: 45
Content-Type: text/html; charset=UTF-8
X-ORACLE-DMS-ECID: 176df710-a1ae-4554-9c30-5bf795293f7f-0000003b
X-ORACLE-DMS-RID: 0

HTTP method POST is not supported by this URL%

【问题讨论】:

    标签: curl redirect servlets postman httpresponse


    【解决方案1】:

    我想通了。如果有人在这里徘徊,这是解决方案。 (请随时提供任何其他解决方案)

    这是“请求”选项在跟踪重定向时产生的问题。

    curl -iL --request POST '127.0.0.1:7001/WebServletApp/employee'

    来自“男人卷曲”

    The method string you set with -X, --request will  be  used  for
    all  requests,  which  if you for example use -L, --location may
    cause unintended side-effects when curl doesn't  change  request
    method according to the HTTP 30x response codes - and similar.
    

    关于删除“--request”选项并将 curl 命令/语法更改为 curl --location -i 'localhost/WebServletApp/employee'。我让它与预期的输出一起工作。

    curl --location -i 'localhost/WebServletApp/employee' \
    --header 'User-Agent: PostmanRuntime/7.26.8' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'firstName=Harshit' \
    --data-urlencode 'lastName=Rajput' \
    --data-urlencode 'emailId=harajput@gmail.com' \
    --data-urlencode 'password=yolo!'
    
    HTTP/1.1 302 Moved Temporarily
    Date: Wed, 19 May 2021 06:34:48 GMT
    Server: Apache/2.4.46 (Unix)
    Location: http://localhost:80/WebServletApp/defaultEmployee?r=18
    Content-Length: 305
    X-ORACLE-DMS-ECID: d0da2790-1291-46f2-b9f4-52355bbc9c1a-00000019
    X-ORACLE-DMS-RID: 0
    Content-Type: text/html
    
    HTTP/1.1 200 OK
    Date: Wed, 19 May 2021 06:34:50 GMT
    Server: Apache/2.4.46 (Unix)
    Content-Length: 252
    X-ORACLE-DMS-ECID: d0da2790-1291-46f2-b9f4-52355bbc9c1a-0000001a
    X-ORACLE-DMS-RID: 0
    
    <html><body><p> firstName employee:: default fname</p><p> lastName employee:: default lname</p><p> firstName employee:: default email</p><p> firstName employee:: default passwd</p><p> Time :: 1621406090386</p><p> Random number r :: 18</p></body></html>%
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      相关资源
      最近更新 更多