post with form-params and content-type json
See original GitHub issueI am programming against an internal REST API that requires a POST, with form-params, and content-type json.
I’ve verified that I can connect to the API, login, get cookies, and do other simple things. I have written working code against this API in Python and have access to similar Ruby code.
Here are two tries. One using clj-http (which works) and the other http-kit, which does not.
(defn post-weblink-http-kit [path cookies spot-id url title description]
@(http-kit/post (str path "spots/" spot-id "/items")
{:headers {"Cookie" cookies}
:content-type :json ; Ignored??
:form-params {:item {:webLink {:url url :title title :description description}}}}))
(defn post-weblink-clj-http [path cookies spot-id url title description]
(client/post (str path "spots/" spot-id "/items")
{:cookies cookies
:content-type :json
:form-params {:item {:webLink {:url url :title title :description description}}}}))
(Note, the only real difference here is that the cookies for http-kit are raw and the cookies for clj-http are a map. But I have verified that is not the issue.)
I have also tried explicitly constructing the json (which is what I needed to do in Python).
(defn post-weblink-json [path cookies spot-id url title description]
@(http-kit/post (str path "spots/" spot-id "/items")
{:headers {"Cookie" cookies}
:content-type :json ; Ignored??
:form-params {:item (json/generate-string {:webLink {:url url :title title :description description}})}}))
Both http-kit calls give an error like this:
:body “"757: unexpected token at ‘item=%7B%22webLink%22%3A%7B%22url%22%3A%22http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNikola_Tesla%22%2C%22title%22%3A%22Nik+Tesla%22%2C%22description%22%3A%22foo+bar+baz%22%7D%7D’"”
Any thoughts on this??
Issue Analytics
- State:
- Created 10 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Unfortunately, if you pass in form-params, http-kit tries to coerce it as below:
so even though you constructed json, it tried to coerce it into a url encoded string. The :content-type was ignored as you rightly point out.
The trick is to pass the :body and :headers explicitly as below:
That said, I agree that the interface of http-kit could be a little friendlier.
Closing this, thanks for the docs PR!