go - 如何使用gorequest发送二进制数据

标签 go

我正在尝试通过 gorequest PUT 方法发送 html 文件的内容。在我尝试联系的服务文档中提到,正文类型应该是 Content-Type: application/octet-stream.

当我执行时:

req.Send(string(content))

其中内容是字节 slice ([]byte),我的 html 文件已损坏,因为文件的内容已编码,所有空格、<、> 等特殊字符都被替换了。 当我执行时:

req.Send(content)

我看到发送了以下内容:[60,104,116,109, .... ] 这不是我所期望的。你能告诉我如何使用 gorequest 将 html 文件作为字节流传输到 web 服务吗?

最佳答案

在发送字符串之前将 req.BounceToRawString 设置为 true 或将内容类型更改为 text/plain

来自 godoc.org 的文档:

If you have set Type to text or Content-Type to text/plain, content will be sent as raw string in body instead of form

关于go - 如何使用gorequest发送二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35017735/

相关文章:

go - 选择 channel 时陷入死锁

json - 为非内置类型定义自定义解码

go - net/http 服务器在 ab (ApacheBench) 重负载下卡住

go - 如何在 Golang 中使用 'time.After' 和 'default'?

arrays - 在 Golang 中替换数组结构中的数据

go - 具有多个 channel 的多个 goroutine 的死锁

go - 修改原生 Golang 包后如何重建它们?

go - 所有的 goroutines 都睡着了——僵局!与 WaitGroup

go - 如何使用 Go 将不同域的请求多路复用到不同的服务器?

android - native 库没有从 golang 条件实现加载到 apex_defaults?