purescript-contrib / purescript-affjax

An asynchronous AJAX library built using Aff.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] Contents of `content` in `AffjaxRequest` are ignored

fosskers opened this issue · comments

Somewhere in the depths of the affjax function, the contents of the content field are being dropped.

I have this run-of-the-mill affjax call:

getKanji :: forall e. Aff ( ajax :: AJAX, console :: CONSOLE | e ) (Either String Analysis)
getKanji = do
  let headers = [ Accept (MediaType "application/json")
                , ContentType (MediaType "text/plain; charset=UTF-8") ]
  res <- affjax $ defaultRequest { url     = "/kanji"
                                 , method  = Left GET
                                 , headers = headers
                                 , content = Just "hi there!" }
  log $ "RESPONSE STATUS: " <> show res.status
  pure $ Aeson.decodeJson res.response

and this simple servant endpoint to answer it:

type API = ... :<|> "kanji" :> ReqBody '[PlainText] Text :> Get '[JSON] Analysis

The handler just echoes what it was given and yields a dummy value for testing:

server :: Server API
server = ...
    :<|> (\t -> say ("GOT: " <> t) *> say ("SIZE: " <> tshow (T.length t)) *> pure dummy)

Invoking the affjax call from above prints the following on the server:

GOT: 
SIZE: 0

I first noticed this when trying to send over some Json as application/json. My server was claiming it had been given no input, and so couldn't parse any JSON.

Any idea why affjax is dumping the content it was given?

If you just attempt to do it directly with XHR in JS does it work?

var xhr = new XMLHttpRequest();
xhr.open("GET", "/kanji");

xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "text/plain; charset=UTF-8");

xhr.onload = function () {
  console.log(xhr.response)
};

xhr.send("hi there!");

@garyb discovered that giving a request body to GET is evil, and so I shouldn't have expected it to work.

Switching the /kanji call to use POST instead fixes everything.