如何使用wreq发送PUT请求?

3

这是我希望使用 wreq 发送 PUT 请求的方式:

{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
main = put "http://httpbin.org/put" ["foo":=(1::Int)]

然而,这会导致以下错误:
HttpExceptionRequest Request {
  host                 = "httpbin.org"
  port                 = 80
  secure               = False
  requestHeaders       = [("Content-Type","application/x-www-form-urlencoded"),("User-Agent","haskell wreq-0.5.3.2")]
  path                 = "/put"
  queryString          = ""
  method               = "POST"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
}
 (StatusCodeException (Response {responseStatus = Status {statusCode = 405, statusMessage = "METHOD NOT ALLOWED"}, responseVersion = HTTP/1.1, responseHeaders = [("Date","Fri, 07 Feb 2020 19:26:28 GMT"),("Content-Type","text/html"),("Content-Length","178"),("Connection","keep-alive"),("Server","gunicorn/19.9.0"),("Allow","PUT, OPTIONS"),("Access-Control-Allow-Origin","*"),("Access-Control-Allow-Credentials","true")], responseBody = (), responseCookieJar = CJ {expose = []}, responseClose' = ResponseClose}) "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n")

很显然从错误信息可以看出,wreq 发送了一个POST请求,尽管我使用了put方法。 我该如何让 wreq 真正发送一个PUT请求呢?
1个回答

4

我认为问题在于,如果您尝试使用[FormParam]进行put操作,则该方法将被覆盖并更改为POST。如果您使用不同类型的载荷进行put操作:

main = put "http://httpbin.org/put" ("xyz" :: ByteString)

然后它就可以正常工作了:

Response {responseStatus = Status {statusCode = 200, statusMessage = "OK"}, ...

我认为这是基础的http-client包中的限制/有意的设计决策,而wreq使用它。当函数urlEncodedBody用于将表单数据打包到主体中时,它会产生一个副作用,即将请求的方法更改为POST

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接