mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 13:23:41 +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 (
|
var (
|
||||||
contentType string
|
contentType string
|
||||||
contentLength int64
|
contentLength int64
|
||||||
|
isStringBody bool
|
||||||
)
|
)
|
||||||
if d == nil || xhr.method == "GET" || xhr.method == "HEAD" {
|
if d == nil || xhr.method == "GET" || xhr.method == "HEAD" {
|
||||||
req, err = http.NewRequest(xhr.method, xhr.url, nil)
|
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)))
|
req, err = http.NewRequest(xhr.method, xhr.url, bytes.NewBufferString(d.(string)))
|
||||||
contentType = "text/plain;charset=UTF-8"
|
contentType = "text/plain;charset=UTF-8"
|
||||||
contentLength = int64(len(d.(string)))
|
contentLength = int64(len(d.(string)))
|
||||||
|
isStringBody = true
|
||||||
case *file.File:
|
case *file.File:
|
||||||
req, err = http.NewRequest(xhr.method, xhr.url, d.(*file.File).Reader)
|
req, err = http.NewRequest(xhr.method, xhr.url, d.(*file.File).Reader)
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
@@ -196,7 +198,8 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.Header = xhr.requestHeaders
|
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)
|
req.Header.Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
if contentLength > 0 {
|
if contentLength > 0 {
|
||||||
|
|||||||
Reference in new issue
Block a user