Skip to content

Commit 0180088

Browse files
author
Andrew Cady
committed
Make JSON encoding optional
This enables the generated javascript to do multipart MIME file uploads. It is the user's responsibility to generate a valid body; e.g.: var fileObject = new File([blob], 'video.webm', { type: 'video/webm' }); var formData = new FormData(); formData.append('video', fileObject); postVideo(formData, onSuccess, onError);
1 parent 46ff335 commit 0180088

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

servant-js.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ library
4747
, base-compat >= 0.9
4848
, charset >= 0.3
4949
, lens >= 4
50-
, servant-foreign >= 0.9 && <0.12
50+
, servant-foreign >= 0.11.2 && <0.12
5151
, servant >= 0.9 && <0.15
5252
, text >= 1.2 && < 1.3
5353

src/Servant/JS/Vanilla.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ generateVanillaJSWith opts req = "\n" <>
3434
<> " xhr.open('" <> decodeUtf8 method <> "', " <> url <> ", true);\n"
3535
<> reqheaders
3636
<> " xhr.setRequestHeader('Accept', 'application/json');\n"
37-
<> (if isJust (req ^. reqBody) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "")
37+
<> (if isJust (req ^. reqBody) && (req ^. reqBodyIsJSON) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "")
3838
<> " xhr.onreadystatechange = function () {\n"
3939
<> " var res = null;\n"
4040
<> " if (xhr.readyState === 4) {\n"
@@ -79,7 +79,7 @@ generateVanillaJSWith opts req = "\n" <>
7979

8080
dataBody =
8181
if isJust (req ^. reqBody)
82-
then "JSON.stringify(body)"
82+
then if (req ^. reqBodyIsJSON) then "JSON.stringify(body)" else "body"
8383
else "null"
8484

8585

0 commit comments

Comments
 (0)