The ajax commands in the main API can take arbitrary maps as request and response formats. Indeed, ajax-request requires that you do this rather than use keywords as formats. This file documents the behaviour.
A request format (given by :format) has two keys:
:content-typeThe content type to send to the server:writeA function taking:paramsand returning a string
A response format (given by :response-format) is a bit more complex:
:descriptionA description of the format, for use in error messages.:readA function that takes the underlyinggoog.net.XhrIoand converts it to a response. Exceptions thrown by this will be caught.:content-typeThe content type to put in theAcceptheader.:typeOptional. One of:blob,:document,:json(which uses the browser's JSON decoding to a JS object),:text(same as blank),:arraybuffer. Set:readto-bodyif you want to use this.
There are functions that return request and response formats. Most of these functions don't take parameters. These correspond to the keywords mentioned in the main documentation.
| Keyword | Request | Response |
|---|---|---|
:transit |
transit-request-format |
transit-response-format |
:json |
json-request-format |
json-response-format |
:url |
url-request-format |
|
:raw |
raw-response-format |
|
:text |
text-request-format |
text-response-format |
:detect |
detect-response-format |
text-response-format and raw-response-format are identical in Clojurescript, but raw-response-format returns the byte stream in Clojure, while text-response-format returns a string. text-request-format is a pass-through in Clojurescript, but converts a string to a byte stream in Clojure (which is what you want).
transit-request-format takes one parameter: writer, which is the transit writer. If you don't supply one, it'll create a default one.
transit-response-format has two parameters: reader which is the transit reader and :raw which, if true, causes a JS object to be returned rather than a CLJS object.
json-response-format takes two parameters
:prefixis a string that needs to be stripped off the front of the response before parsing it as JSON, which is useful for dealing with external APIs that put things likewhile(1);in front. (And if you're using cljs-ajax withGET, learn about cross-site scripting and then employ this feature in your own code.) Defaults tonil.:keywords?, which if true returns the keys as keywords and if false or unprovided returns them as strings.:raw, if true, returns a JS object rather than a CLJS object.
detect-response-format has one parameter: :defaults, which is a list of pairs. The first item in the pair is a substring that starts the content type. The second item is the response format function to call. It will be passed the options in. So, you can, for instance, have :raw set to true and content detection available at the same time. If you use the zero-arity version, :defaults is set to default-formats.
EDN is deprecated, but the functions edn-request-format and edn-response-format are available in the ajax.edn namespace.
To get the raw XhrIo object back:
{:read identity :description "raw"}Incidentally, if anyone wants to implement a response format that returns the response in a ring-style format, I accept pull requests.