gpt4 book ai didi

http - 如何使用 wreq 发送 PUT 请求?

转载 作者:行者123 更新时间:2023-12-01 12:03:02 28 4
gpt4 key购买 nike

以下是我希望能够使用 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 请求?

最佳答案

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

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

然后它工作正常:

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

我认为这是 wreq 使用的底层 http-client 包中的限制/有意设计决策。当函数urlEncodedBody用于将表单数据打包到主体中,它具有将请求方法更改为 POST 的副作用。

关于http - 如何使用 wreq 发送 PUT 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60119905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com