mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-06 00:25:43 +00:00
fix: xhr string request body can specify Content-Type (#596)
This commit is contained in:
1 file changed
+4
-1
@@ -154,6 +154,7 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
||||
var (
|
||||
contentType string
|
||||
contentLength int64
|
||||
isStringBody bool
|
||||
)
|
||||
if d == nil || xhr.method == "GET" || xhr.method == "HEAD" {
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, nil)
|
||||
@@ -163,6 +164,7 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, bytes.NewBufferString(d.(string)))
|
||||
contentType = "text/plain;charset=UTF-8"
|
||||
contentLength = int64(len(d.(string)))
|
||||
isStringBody = true
|
||||
case *file.File:
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, d.(*file.File).Reader)
|
||||
contentType = "application/octet-stream"
|
||||
@@ -196,7 +198,8 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
||||
return
|
||||
}
|
||||
req.Header = xhr.requestHeaders
|
||||
if contentType != "" {
|
||||
// Only string body can specify Content-Type header by user
|
||||
if contentType != "" && (!isStringBody || req.Header.Get("Content-Type") == "") {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
if contentLength > 0 {
|
||||
|
||||
Reference in new issue
Block a user