[英]Is postWith switching my request's Content-Type?
无论我输入什么值作为请求的“内容类型”,我发出的传出请求似乎都将其替换为“application/x-www-form-urlencoded”。 我试图点击的应用程序需要“application/json”。 我的代码基本上如下。
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
...
submissionResources = ["https://widgets.example.com/v2/widgets/request"]
sendWidgetToEndpoint submissionResources workingToken key widgetArray = do
let opts = defaults & header "Content-Type" .~ ["application/json"]
& header "apikey" .~ [key]
& header "Authorization" .~ [workingToken]
endPointURL = head submissionResources
widgetId = widgetArray !! 0
numberOfWidgets = widgetArray !! 1
widgetText = widgetArray !! 2
submissionResult <- postWith opts endPointURL [ "widgetId" := widgetId
, "numWidgets" := numberOfWidgets
, "widgetText" := widgetText
]
return submissionResult
我的问题是我不断从这个端点返回Status {statusCode = 415, statusMessage = "Unsupported Media Type"}
,我相信这是因为我发送的请求似乎覆盖了“Content-Type”我的头。 我曾尝试使用“application/json”和“text/plain”,但我得到的响应总是向我表明我发送的所有标头都按预期显示,除了 Content-Type 总是变成“application/x-www” -form-urlencoded”。
如何确保 wreq 在我的请求标头中保留“Content-Type: application/json”?
编辑:我正在通过 API 服务器在回复我时告诉我的内容来确定原始请求中的标头。
代码段中postWith
的最后一个参数的类型是[FormParam]
,该类型强制 Content-Type 进行 urlencoded。
要发送 JSON,请发送Value
或Encoding
类型的内容(来自Data.Aeson
)。
import Data.Aeson (pairs, (.=))
...
-- also remove the "Content-Type" field from opts
submissionResult <- postWith opts endpointURL $ pairs
( "widgetId" .= widgetId <>
"numWidgets" .= numberOfWidgets <>
"widgetText" .= widgetText )
...
Content-Type 由您通过Postable
实例传递给postWith
的有效负载设置。 如果您想使用另一个 Content-Type 标头,请使用Postable
实例定义您自己的类型,您可以在其中设置适当的 Content-Type。 您也可以选择不在Postable
实例中设置任何 Content-Type,因此您可以通过选项进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.