From d530f429af6c33f69cb396442a2cebc9a132039a Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 26 Aug 2014 14:11:22 -0400 Subject: [PATCH] use POST requests for all searches due to HTTP GET query size limits --- src/clj_hl7_fhir/core.clj | 37 ++++++++++++++++++++++++------------- src/clj_hl7_fhir/util.clj | 9 +++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index 0566ee9..274130c 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -13,29 +13,31 @@ (and (map? response) (.contains (get-in response [:headers "Content-Type"]) "application/json+fhir"))) -(defn- fhir-request [type base-url resource-url & {:keys [params body follow-location?]}] +(defn- fhir-request [type base-url resource-url & {:keys [params body params-as-body? follow-location?]}] (let [query (map->query-string params) - url (build-url base-url resource-url query) + url (build-url base-url resource-url (if-not params-as-body? query)) + body (if params-as-body? query body) follow-location? (if (nil? follow-location?) true follow-location?)] (try - (let [response (case type - :get (http-get-json url) - :post (http-post-json url body) - :put (http-put-json url body) - :delete (http-delete-json url body)) - body (:body response) - location (get-in response [:headers "Location"])] + (let [response (case type + :get (http-get-json url) + :form-post (http-post-form url body) + :post (http-post-json url body) + :put (http-put-json url body) + :delete (http-delete-json url body)) + response-body (:body response) + location (get-in response [:headers "Location"])] (if location (if follow-location? (-> (http-get-json location) :body (json/parse-string true)) (if (fhir-response? response) - (json/parse-string body true) + (json/parse-string response-body true) location)) (if (fhir-response? response) - (json/parse-string body true) - body))) + (json/parse-string response-body true) + response-body))) (catch ExceptionInfo ex (let [{:keys [status body] :as response} (:object (ex-data ex)) fhir-resource-response? (fhir-response? response)] @@ -355,14 +357,19 @@ the results of this function can be passed to fetch-next-page or fetch-all to collect resources returned in paged search results easier. an exception is thrown if an error response is received. + to overcome HTTP GET query size limitations that could be an issue for search operations with + a large number of parameters, all search requests are submitted as + application/x-www-form-urlencoded HTTP POST requests. + reference: search: http://hl7.org/implement/standards/fhir/http.html#search" [base-url type where & params] (let [resource-name (->fhir-resource-name type) url-components ["/" resource-name "/_search"]] - (fhir-request :get + (fhir-request :form-post base-url (apply join-paths url-components) + :params-as-body? true :params (merge (search-params->query-map where) (apply hash-map (if (and (seq? params) @@ -457,3 +464,7 @@ ;(def server-url "http://fhir.healthintersections.com.au/open") ;(def server-url "http://spark.furore.com/fhir") ;(def server-url "http://fhirtest.uhn.ca/base") + + +(clojure.pprint/pprint + (get-resource server-url :patient 38)) \ No newline at end of file diff --git a/src/clj_hl7_fhir/util.clj b/src/clj_hl7_fhir/util.clj index c28c6a1..3a0e691 100644 --- a/src/clj_hl7_fhir/util.clj +++ b/src/clj_hl7_fhir/util.clj @@ -78,6 +78,15 @@ (map? body) {:body (json/generate-string body)} (string? body) {:body body})))) +(defn http-post-form [url & [body]] + (http-request + http/post url + (merge + {:content-type "application/x-www-form-urlencoded"} + (cond + (map? body) {:body (json/generate-string body)} + (string? body) {:body body})))) + (defn http-put-json [url & [body]] (http-request http/put url