net/http: HTTP/1.x传输连接中断:http: ContentLength=2514,但Body长度为0。

huangapple go评论78阅读模式
英文:

net/http: HTTP/1.x transport connection broken: http: ContentLength=2514 with Body length 0

问题

我正在尝试将一个Node.js应用程序转换为Go。在这里,我正在尝试将文件上传到B2。但是我得到了一个错误信息:Post "https://pod-XX.backblaze.com/b2api/v2/b2_upload_file/WERTGVWGTE/cSEREf": net/http: HTTP/1.x transport connection broken: http: ContentLength=3312 with Body length 0。以下是我的代码:

// 打开文件
file, err := os.Open(location)
if err != nil {
    log.Fatal(err)
    return "", err
}
defer file.Close()

// 创建文件的SHA1哈希值
hash := sha1.New()
if _, err := io.Copy(hash, file); err != nil {
    log.Fatal(err)
    return "", err
}
sha1Sum := hex.EncodeToString(hash.Sum(nil))

// 创建HTTP客户端
client := http.Client{}
req, _ := http.NewRequest("POST", b2.UploadUrl, file)
contentLength, _ := file.Stat()

req.ContentLength = contentLength.Size()  // 如果没有这一行,会抛出map[code:bad_request message:Missing header: Content-Length status:400]错误
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Authorization", b2.AuthorizationToken)
req.Header.Set("X-Bz-File-Name", name)
req.Header.Set("X-Bz-Content-Sha1", sha1Sum)

res, errr := client.Do(req)
if errr != nil {
    log.Fatalln(errr)
    return "", errr
}

仅供参考,以下是完全正常工作的Node.js代码:

const file = await fs.readFile(location);
const sha1 = crypto.createHash('sha1').update(file).digest("hex");
const config = {
    headers: {
        'Authorization': auth_token,
        'X-Bz-File-Name': final_name,
        'Content-Type': 'b2/x-auto',
        'X-Bz-Content-Sha1': sha1
    }
};
let response = await axios.post(upload_url, file, config);
return response.data['fileId'];

希望对你有所帮助!如果你有任何其他问题,请随时问我。

英文:

I am trying to convert a nodejs app to Go. Here I am trying to upload a file to B2. But I am getting Post "https://pod-XX.backblaze.com/b2api/v2/b2_upload_file/WERTGVWGTE/cSEREf": net/http: HTTP/1.x transport connection broken: http: ContentLength=3312 with Body length 0 . Here is my code:


// open file
	file, err := os.Open(location)
	if err != nil {
		log.Fatal(err)
		return "", err
	}
	defer file.Close()

	// create sha1 hash of file
	hash := sha1.New()
	if _, err := io.Copy(hash, file); err != nil {
		log.Fatal(err)
		return "", err
	}
	sha1Sum := hex.EncodeToString(hash.Sum(nil))



	// http client
	client := http.Client{}
	req, _ := http.NewRequest("POST", b2.UploadUrl, file)
	contentLength, _ := file.Stat()
	
	req.ContentLength = contentLength.Size()  // without this, its throwing map[code:bad_request message:Missing header: Content-Length status:400]
	req.Header.Set("Content-Type", "application/octet-stream")
	req.Header.Set("Authorization", b2.AuthorizationToken)
	req.Header.Set("X-Bz-File-Name", name)
	req.Header.Set("X-Bz-Content-Sha1", sha1Sum)

	res , errr := client.Do(req)
	if errr != nil {
		log.Fatalln(errr)
		return "", errr
	}

Just for reference here is the nodejs code that is perfectly working:

const file = await fs.readFile(location);
    const sha1 = crypto.createHash('sha1').update(file).digest("hex");
    const config = {
        headers: {
            'Authorization': auth_token,
            'X-Bz-File-Name': final_name,
            'Content-Type': 'b2/x-auto',
            'X-Bz-Content-Sha1': sha1
        }
    };
    let response = await axios.post(upload_url, file, config);
    return response.data['fileId'];

答案1

得分: 1

你在这里使用了文件:

if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }

可以使用func (*File) Seek或将文件加载到内存中。

英文:

You have consumed file here:

if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }

Use func (*File) Seek or load file to memory.

huangapple
  • 本文由 发表于 2022年3月14日 16:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/71464833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定