How to upload file using golang code

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

How to upload file using golang code

问题

使用golang代码将文件传输到远程服务,使用其API。他们的要求是请求不能使用multipart/form-data。我尝试了以下curl命令:

curl -i -X PUT -F filedata=@text.txt -H "Content-Type: text/plain"  https://url.of.endpoint.com

由于它模拟了表单,所以它不起作用,但是这个命令:

curl -i -X PUT -T text.txt -H "Content-Type: text/plain"  https://url.of.endpoint.com

可以正常工作。

我该如何将这个curl命令翻译成golang代码?

英文:

With golang code I have to transfer file to remote service using their api. Their requirement is that request MUST NOT use multipart/form-data. I tried this curl command:

curl -i -X PUT -F filedata=@text.txt -H "Content-Type: text/plain"  https://url.of.endpoint.com

it doesn't work since it simulates form, but this command:

curl -i -X PUT -T text.txt -H "Content-Type: text/plain"  https://url.of.endpoint.com

works perfectly.

How can I translate this curl command to golang code?

答案1

得分: 11

你需要创建一个"PUT"请求,并将其请求体设置为文件的内容:

package main

import (
	"log"
	"net/http"
	"os"
)

func main() {
	data, err := os.Open("text.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	req, err := http.NewRequest("PUT", "http://localhost:8080/test.txt", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", "text/plain")

	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
}

请注意,这是一个使用Go语言编写的示例代码,用于创建一个"PUT"请求并将文件内容发送到指定的URL。你需要将代码中的文件路径和URL替换为你自己的值。

英文:

You have to create a "PUT" request and set its request body to the contents of the file:

package main

import (
	"log"
	"net/http"
	"os"
)

func main() {
	data, err := os.Open("text.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	req, err := http.NewRequest("PUT", "http://localhost:8080/test.txt", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", "text/plain")

	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
}

答案2

得分: 1

代码如下:

package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"os"
)

func main() {
	// 打开一个文件。
	f, err := os.Open("text.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// 将文件发送到指定的URL。
	resp, err := http.Post("https://url.of.endpoint.com", "text/plain", f)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// 将响应内容输出以进行调试。
	b, err := httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}
	fmt.Println(b)
}

希望对你有帮助!

英文:

It would be like this:

package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"os"
)

func main() {
	// Open a file.
	f, err := os.Open("text.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// Post a file to URL.
	resp, err := http.Post("https://url.of.endpoint.com", "text/plain", f)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// Dump response to debug.
	b, err := httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}
	fmt.Println(b)
}

huangapple
  • 本文由 发表于 2014年12月26日 20:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/27656898.html
匿名

发表评论

匿名网友

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

确定