如何在 httpkit 中将 Content-Type 设置为 application/json
How to set Content-Type as application/json in httpkit
我使用 httpkit 作为 http 客户端。我尝试了很多解决方案来使 header Content-Type
成为 application/json
,但都失败了。
这是我的代码:
(require '[org.httpkit.client :as http])
(http/post
url
{ :query-params {"q" "foo, bar"}
:form-params {"q" "foo, bar"}
:headers {"Content-Type" "application/json; charset=utf-8"}})
Post 使用上面的代码会得到 response status 200
但 Content-Type
是 application/x-www-form-urlencoded
。
如果删除行 application/x-www-form-urlencoded
,将得到 response status 400
。
PS: 我把flask作为web服务器。
我没用过httpkit
(可以推荐clj-http
)但我认为你应该使用:body
选项而不是:form-params
来指定payload为后者将 force the Content-Type
to application/x-www-form-urlencoded
. It makes sense considering how form params 在 HTTP 中工作。
您想发送什么请求?
:query-params
会将 ?q=foo,bar
附加到请求 url
:form-params
将使 body q=foo,bar
与 content-type application/x-www-form-urlencoded
这可能超出了需要,但是 body 也必须是 application/json
,除了已经是 application/x-www-form-urlencoded
。
如果你想要 json body,你可以这样做:
:body (clojure.data.json/write-str {:q "foo,bar"})
我使用 httpkit 作为 http 客户端。我尝试了很多解决方案来使 header Content-Type
成为 application/json
,但都失败了。
这是我的代码:
(require '[org.httpkit.client :as http])
(http/post
url
{ :query-params {"q" "foo, bar"}
:form-params {"q" "foo, bar"}
:headers {"Content-Type" "application/json; charset=utf-8"}})
Post 使用上面的代码会得到 response status 200
但 Content-Type
是 application/x-www-form-urlencoded
。
如果删除行 application/x-www-form-urlencoded
,将得到 response status 400
。
PS: 我把flask作为web服务器。
我没用过httpkit
(可以推荐clj-http
)但我认为你应该使用:body
选项而不是:form-params
来指定payload为后者将 force the Content-Type
to application/x-www-form-urlencoded
. It makes sense considering how form params 在 HTTP 中工作。
您想发送什么请求?
:query-params
会将?q=foo,bar
附加到请求 url:form-params
将使 bodyq=foo,bar
与 content-typeapplication/x-www-form-urlencoded
这可能超出了需要,但是 body 也必须是 application/json
,除了已经是 application/x-www-form-urlencoded
。
如果你想要 json body,你可以这样做:
:body (clojure.data.json/write-str {:q "foo,bar"})