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

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

Golang - Sending API POST Request - Not enough arguments error

问题

以下是翻译好的内容:

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

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

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

  1. //main.go
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. )
  10. // RequestDetails 包含Send的输入数据
  11. type RequestDetails struct {
  12. EndPoint string
  13. FormType string
  14. FormData map[string]string
  15. }
  16. // Send 向端点发送API POST请求
  17. func (rd RequestDetails) Send(w http.ResponseWriter, r *http.Request) {
  18. json_data, err := json.Marshal(rd.FormData)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println(resp)
  27. }
  28. func main() {
  29. m := map[string]string{
  30. "AuthParamOne": "AP0000001",
  31. "AuthParamTwo": "AP0000002",
  32. "AuthParamThree": "AP0000003",
  33. }
  34. reqDetails := RequestDetails{
  35. EndPoint: "https://httpbin.org/post",
  36. FormType: "application/json",
  37. FormData: m,
  38. }
  39. http.HandleFunc(reqDetails.Send())
  40. }
英文:

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.

  1. go run main.go
  2. # command-line-arguments
  3. ./main.go:53:17: not enough arguments in call to http.HandleFunc
  4. ./main.go:53:33: not enough arguments in call to reqDetails.Send
  5. have ()
  6. want (http.ResponseWriter, *http.Request)
  7. ./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.

  1. //main.go
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. )
  10. // RequestDetails contains input data for Send
  11. type RequestDetails struct {
  12. EndPoint string
  13. FormType string
  14. FormData map[string]string
  15. }
  16. // Send sends API POST request to an endpoint
  17. func (rd RequestDetails) Send(w http.ResponseWriter, r *http.Request) {
  18. json_data, err := json.Marshal(rd.FormData)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println(resp)
  27. }
  28. func main() {
  29. m := map[string]string{
  30. "AuthParamOne": "AP0000001",
  31. "AuthParamTwo": "AP0000002",
  32. "AuthParamThree": "AP0000003",
  33. }
  34. reqDetails := RequestDetails{
  35. EndPoint: "https://httpbin.org/post",
  36. FormType: "application/json",
  37. FormData: m,
  38. }
  39. http.HandleFunc(reqDetails.Send())
  40. }

答案1

得分: 3

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

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

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

  1. 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:

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

for code above follow this:

  1. 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,所以看起来你不需要它们:

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

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

  1. reqDetails.Send()

整个main.go文件如下:

  1. //main.go
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. )
  10. // RequestDetails包含Send方法的输入数据
  11. type RequestDetails struct {
  12. EndPoint string
  13. FormType string
  14. FormData map[string]string
  15. }
  16. // Send向端点发送API POST请求
  17. func (rd RequestDetails) Send() {
  18. json_data, err := json.Marshal(rd.FormData)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println(resp)
  27. }
  28. func main() {
  29. m := map[string]string{
  30. "AuthParamOne": "AP0000001",
  31. "AuthParamTwo": "AP0000002",
  32. "AuthParamThree": "AP0000003",
  33. }
  34. reqDetails := RequestDetails{
  35. EndPoint: "https://httpbin.org/post",
  36. FormType: "application/json",
  37. FormData: m,
  38. }
  39. reqDetails.Send()
  40. }
英文:

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

  1. 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:

  1. reqDetails.Send()

The whole main.go file:

  1. //main.go
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. )
  10. // RequestDetails contains input data for Send
  11. type RequestDetails struct {
  12. EndPoint string
  13. FormType string
  14. FormData map[string]string
  15. }
  16. // Send sends API POST request to an endpoint
  17. func (rd RequestDetails) Send() {
  18. json_data, err := json.Marshal(rd.FormData)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. resp, err := http.Post(rd.EndPoint, rd.FormType, bytes.NewBuffer(json_data))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println(resp)
  27. }
  28. func main() {
  29. m := map[string]string{
  30. "AuthParamOne": "AP0000001",
  31. "AuthParamTwo": "AP0000002",
  32. "AuthParamThree": "AP0000003",
  33. }
  34. reqDetails := RequestDetails{
  35. EndPoint: "https://httpbin.org/post",
  36. FormType: "application/json",
  37. FormData: m,
  38. }
  39. reqDetails.Send()
  40. }

答案3

得分: 0

如果你的代码像这样:

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

你需要这样来读取一行:

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

if your code like this

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

you needed this for reading line line

  1. old -> input, _ := watcher.ReadString()
  2. 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:

确定