WebSharper

I want to send FormData via Ajax but the endpoint is not recognized.

Here's the code:

1
2
3
4
5
type Arguments =
	{ [<FormData>] Something: string }

type MyEndPoint =  
  | [<EndPoint "POST /upload">] Upload of Arguments

This work for html form submits to /upload but it does not work for ajax submit even though I can see the payload being posted as form data.

1
2
3
4
5
------WebKitFormBoundaryzW7tLcyXj4AGC18r
Content-Disposition: form-data; name="Something"

hello world
------WebKitFormBoundaryzW7tLcyXj4AGC18r--

Am I doing something wrong?

  • loic.denuziere

    This might be a bug in the handling of the multipart/form-data content type. Does this still fail if you try to use application/x-www-form-urlencoded as the contentType?

    • Lamk

      Hi Loïc, application/x-www-form-urlencoded works via Ajax post and string data Something=hello.

      In my case, I will need to append a file to the form-data together with more input fields. Therefore I was hoping to be able to get the content deserialised into the record type and extract the file from the context request.

      Is it possible to have access to the form data from the context?

    • Lamk

      After further investigation, I figured why it didn't work.

      The problem is that the AjaxSettings.ContentType accepts a string option but in order to submit a form-data via ajax, it needs to be set to false (bool typed).

      I've tried two things:

      • setting the ContentType to "false" but it simply sends the POST request with content-type="false"
      • setting the ContentType toNone but it simply doesn't include it and therefore the content type becomes the default application/x-www-form-urlencoded

      I can see from the JQuery definition: "contentType", (T<string> + T<bool>) but I don't seem to be able to set the content type to false.

      So I guess my question changed to - How can I set the AjaxSettings.ContentType to false in order to have the correct content type multipart/form-data; boundary=XXXXX set in the POST request?

      • loic.denuziere

        Ha, interesting timing: we actually just fixed this and haven't published it yet :) This is the version currently on NuGet, and it is only T<string>. So the next version of WebSharper 4 will have this parameter as Choice<string, bool>. To get this working right now, you can simply use As<string> false.

        • Lamk

          Nice, thanks Loïc, I was setting it with settings?contentType <- false but As<string> false works too.