dakrone / clj-http

An idiomatic clojure http client wrapping the apache client. Officially supported version.
http://clojars.org/clj-http
MIT License
1.79k stars 410 forks source link

`clj-http.headers.HeaderMap` is not fully compatible with normal Clojure maps #525

Open jsyrjala opened 5 years ago

jsyrjala commented 5 years ago

Maps returned by header-map (HeaderMap instance) are not fully compatible with normal Clojure maps. These maps are part of the clj-http reponse data.

Not sure what if anything should be done to fix this.

=> 
(clojure.set/rename-keys (into (clj-http.headers/header-map) {"Foo" "Bar"}) 
                         {"Foo" :foo})
{"Foo" "Bar"}
=> 
(clojure.set/rename-keys (into {} {"Foo" "Bar"}) 
                         {"Foo" :foo})
{:foo "Bar"}
dakrone commented 4 years ago

Hmm okay, we need to figure out what rename-keys uses that the HeaderMap needs to implement in order to get it to work.

jsyrjala commented 4 years ago

rename-keys uses assoc internally. assoc behaves like this: HeaderMap converts keyword keys to Pascal cased string keys. So maybe this is just a feature of HeaderMap.

(require 'clj-http.headers)
(def a  (into (clj-http.headers/header-map) {"Foo" "Bar"}) )
(assoc a :foo "baz")
=> {"Foo" "baz"}

(assoc a :quux-xyz "baz")
=> {"Foo" "Bar", "Quux-Xyz" "baz"}

(assoc a "foob" "baz")
=> {"Foo" "Bar", "foob" "baz"}
jsyrjala commented 4 years ago

Anyways, you can use (into {} ...) to convert HeaderMap to normal Clojure data structure.

(def a  (into (clj-http.headers/header-map) {"Foo" "Bar"}) )

(def b (into {} a))

(clojure.set/rename-keys b {"Foo" :foo})
=> {:foo "Bar"}