Skip to content

Commit

Permalink
fix: xhr string request body can specify Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jul 5, 2024
1 parent 17c8fb7 commit 3b1aebc
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/download/engine/inject/xhr/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3b1aebc

Please sign in to comment.