-
Notifications
You must be signed in to change notification settings - Fork 161
Fetch request body as application/x-www-form-urlencoded #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for reporting. That is indeed strange, I can't understand why scala-js-dom would do something different than vanilla JavaScript: it is purely a facade for the JavaScript API. What browser are you testing with? Can you try setting the headers in Scala.js like this: Fetch
.fetch(
"/test-api",
new RequestInit {
method = HttpMethod.POST
body = "key1=value1&key2=value2"
headers = js.Dictionary("content-type" -> "application/x-www-form-urlencoded")
}
) This matches your JavaScript code more closely. Similarly, can you try changing the JavaScript code to: var headers = new Headers();
headers.set("content-type", "application/x-www-form-urlencoded");
window.fetch('/test-api', {
method: 'POST',
headers: headers,
body: 'key1=value1&key2=value2'
}) This matches the original Scala.js code that has the problem. Please try those and let me know what happens. Regarding
It seems we have a facade for
|
Thanks @armanbilge for your detail reply, to make it easier testing, I setup a new project to demonstrate this issue here: Button1 is using To run this example, you can run these sbt command: $> sbt
sbt:scalajs-fetch-urlencoded> compile
sbt:scalajs-fetch-urlencoded> webserver/run Open example web at http://localhost:8080/ and click the first button, the fetch request is sent with header while for second button, it is sent correctly with form-urlencoded Thanks for the suggestion with In addition, I'm happy to raise a PR to let |
Thank you for setting up the example! Very helpful. Look forward to the PR :) Glad you have a working solution for now, we'll investigate the problem with |
Ah, this is quite silly actually! This code will work. val myHeaders = new Headers()
myHeaders.set("content-type", "application/x-www-form-urlencoded")
Fetch
.fetch(
"/test-api",
new RequestInit {
method = HttpMethod.POST
body = "key1=value1&key2=value2"
headers = myHeaders
}
)
|
Oh wow, I would never have guessed! :) |
BTW, I think I did a similar thing for a different trait, but one of the scalac warnings got triggered (something like "this thing is doing nothing except referencing itself", might have been scala3) |
Yes, I think also enabling warnings about unused variables (which Scala 2 has) would help catch thisβnot the cyclic reference of course, but the unused
|
@armanbilge wow thanks for your debugging, didn't notice that self-referencing π |
@duchoang btw I noticed you are using http4s in your example project, you may find this project useful: https://github.com/http4s/http4s-dom |
While upgrading ScalaJS from 1.x to 2.x, we are hitting to this issue with sending data with urlencoded type (in version 1.x we were using Ajax, now we must switch to
fetch()
)Example with this simple code:
When executing this script in browser, the
content-type
header of fetch request is changed to betext/plain;charset=UTF-8
. It seems ScalaJs itselves detects the body as string and decide to change the content-type totext/plain
which is wrong.If I use the same code above directly with javascript here (testing by openning the chrome console), the fetch request is sent correctly with content-type urlencoded.
Could you please show me how to use this properly?
While I search some solution for this, I notice one workaround for this is to use this type
URLSearchParams
in the body of fetch request, this will set the content-type automatically to urlencoded. But sadly the type of request bodyRequestInit
doesnt support this yet:scala-js-dom/dom/src/main/scala/org/scalajs/dom/package.scala
Line 35 in e7254c8
The text was updated successfully, but these errors were encountered: