Description
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:
import org.scalajs.dom._
val headers = new Headers()
headers.set("content-type", "application/x-www-form-urlencoded")
Fetch
.fetch(
"/test-api",
new RequestInit {
method = HttpMethod.POST
body = "key1=value1&key2=value2"
headers = headers
}
)
When executing this script in browser, the content-type
header of fetch request is changed to be text/plain;charset=UTF-8
. It seems ScalaJs itselves detects the body as string and decide to change the content-type to text/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.
window.fetch('/test-api', {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
body: 'key1=value1&key2=value2'
})
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 body RequestInit
doesnt support this yet: