2

I use httpkit as http client. I try many solutions to make the header Content-Type be application/json, but all failed.

Here is my code:

(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 with the code above would get response status 200 but Content-Type is application/x-www-form-urlencoded.

And if delete the line application/x-www-form-urlencoded, would get response status 400.

PS: I take flask as web server.

keroro520
  • 459
  • 4
  • 12
  • 4
    Is it possible that you are expecting your **request** `Content-Type` to modify the *response* `Content-Type` header? If so, then you should be looking into the implementation of the endpoint in your web server, not the client. – juan.facorro Jul 01 '15 at 15:46

2 Answers2

2

What request do you want to send?

  • :query-params will attach ?q=foo,bar to the request url
  • :form-params will make the body q=foo,bar with content-type application/x-www-form-urlencoded

That's probably more than needed, but then the body has to be application/json too, besides already being application/x-www-form-urlencoded.

If you want a json body, you could do:

:body (clojure.data.json/write-str {:q "foo,bar"})
schaueho
  • 3,419
  • 1
  • 21
  • 32
Assen Kolov
  • 4,143
  • 2
  • 22
  • 32
1

I haven't used httpkit (can recommend clj-http) but I think you should use the :body option instead of :form-params to specify payload as the latter would force the Content-Type to application/x-www-form-urlencoded. It makes sense considering how form params work in HTTP.

Pol
  • 5,064
  • 4
  • 32
  • 51