Golang – 发送 API POST 请求 – 参数不足错误

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

Golang - Sending API POST Request - Not enough arguments error

问题

以下是翻译好的内容:

以下代码尝试使用位于RequestDetails.FormData中的有效负载发送POST API请求。当我运行main.go函数时,我得到以下错误。

go run main.go
# command-line-arguments
./main.go:53:17: 调用http.HandleFunc时参数不足
./main.go:53:33: 调用reqDetails.Send时参数不足
         ()
        需要 (http.ResponseWriter, *http.Request)
./main.go:53:33: reqDetails.Send() 用作值

以下是代码。有人知道我在这里做错了什么吗?非常感谢您的帮助。

//main.go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

// RequestDetails 包含Send的输入数据
type RequestDetails struct {
	EndPoint string
	FormType string
	FormData map[string]string
}

// Send 向端点发送API POST请求
func (rd RequestDetails) Send(w http.ResponseWriter, r *http.Request) {

	json_data, err := json.Marshal(rd.FormData)

	if err != nil {
		log.Fatal(err)
	}

	resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp)

}

func main() {
	m := map[string]string{
		"AuthParamOne":   "AP0000001",
		"AuthParamTwo":   "AP0000002",
		"AuthParamThree": "AP0000003",
	}

	reqDetails := RequestDetails{
		EndPoint: "https://httpbin.org/post",
		FormType: "application/json",
		FormData: m,
	}

	http.HandleFunc(reqDetails.Send())
}
英文:

The following code attempts to send a POST API request with a payload that is in RequestDetails.FormData. When I run main.go function, then I get the following errors.

go run main.go
# command-line-arguments
./main.go:53:17: not enough arguments in call to http.HandleFunc
./main.go:53:33: not enough arguments in call to reqDetails.Send
        have ()
        want (http.ResponseWriter, *http.Request)
./main.go:53:33: reqDetails.Send() used as value

The code is available below. Anybody knows what I could do wrong here? Thanks a lot for your help.

//main.go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

// RequestDetails contains input data for Send
type RequestDetails struct {
	EndPoint string
	FormType string
	FormData map[string]string
}

// Send sends API POST request to an endpoint
func (rd RequestDetails) Send(w http.ResponseWriter, r *http.Request) {

	json_data, err := json.Marshal(rd.FormData)

	if err != nil {
		log.Fatal(err)
	}

	resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp)

}

func main() {
	m := map[string]string{
		"AuthParamOne":   "AP0000001",
		"AuthParamTwo":   "AP0000002",
		"AuthParamThree": "AP0000003",
	}

	reqDetails := RequestDetails{
		EndPoint: "https://httpbin.org/post",
		FormType: "application/json",
		FormData: m,
	}

	http.HandleFunc(reqDetails.Send())
}

答案1

得分: 3

你必须在下面的代码中使用HandleFunc:

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

对于上面的代码,请按照以下方式操作:

http.HandleFunc("/test", reqDetails.Send) //-> 添加引用而不是调用'reqDetails.Send()'

参考链接:https://pkg.go.dev/net/http#HandleFunc

请投票支持 Golang – 发送 API POST 请求 – 参数不足错误

英文:

you have to use HandleFunc in following below:

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

for code above follow this:

http.HandleFunc("/test",reqDetails.Send) //-> add reference instead of calling 'reqDetails.Send()'

reference: https://pkg.go.dev/net/http#HandleFunc

please vote up Golang – 发送 API POST 请求 – 参数不足错误

答案2

得分: 2

在你的Send方法中,你没有使用w http.ResponseWriter和r *http.Request,所以看起来你不需要它们:

func (rd RequestDetails) Send() {...

另外,在你的最后一行,HandleFunc需要不同的参数,这在你的情况下也是不必要的。只需尝试运行Send方法:

reqDetails.Send()

整个main.go文件如下:

//main.go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

// RequestDetails包含Send方法的输入数据
type RequestDetails struct {
	EndPoint string
	FormType string
	FormData map[string]string
}

// Send向端点发送API POST请求
func (rd RequestDetails) Send() {

	json_data, err := json.Marshal(rd.FormData)

	if err != nil {
		log.Fatal(err)
	}

	resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp)

}

func main() {
	m := map[string]string{
		"AuthParamOne":   "AP0000001",
		"AuthParamTwo":   "AP0000002",
		"AuthParamThree": "AP0000003",
	}

	reqDetails := RequestDetails{
		EndPoint: "https://httpbin.org/post",
		FormType: "application/json",
		FormData: m,
	}

	reqDetails.Send()
}
英文:

In your Send method, you don't make use of w http.ResponseWriter, r *http.Request, So it seems you don't need them:

func (rd RequestDetails) Send() {...

Also in your last line, HandleFunc requires different arguments which once again is not necessary in your case. Just try to run the Send method:

reqDetails.Send()

The whole main.go file:

//main.go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

// RequestDetails contains input data for Send
type RequestDetails struct {
	EndPoint string
	FormType string
	FormData map[string]string
}

// Send sends API POST request to an endpoint
func (rd RequestDetails) Send() {

	json_data, err := json.Marshal(rd.FormData)

	if err != nil {
		log.Fatal(err)
	}

	resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp)

}

func main() {
	m := map[string]string{
		"AuthParamOne":   "AP0000001",
		"AuthParamTwo":   "AP0000002",
		"AuthParamThree": "AP0000003",
	}

	reqDetails := RequestDetails{
		EndPoint: "https://httpbin.org/post",
		FormType: "application/json",
		FormData: m,
	}

	reqDetails.Send()
}

答案3

得分: 0

如果你的代码像这样:

watcher := bufio.NewReader(os.Stdin)
input, _ := watcher.ReadString()    
fmt.Println(input)

你需要这样来读取一行:

old -> input, _ := watcher.ReadString()   
new -> input, _ := watcher.ReadString('\n')
英文:

if your code like this

watcher := bufio.NewReader(os.Stdin)
input, _ := watcher.ReadString()    
fmt.Println(input)

you needed this for reading line line

old -> input, _ := watcher.ReadString()   
new -> input, _ := watcher.ReadString('\n')   

huangapple
  • 本文由 发表于 2022年2月6日 08:52:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71003377.html
匿名

发表评论

匿名网友

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

确定