Reputation: 1
I'm having some troubles trying to execute a POST
using Golang
. With the code below
func Postfunc(w http.ResponseWriter , rep *http.Request) {
var jsonStr = []byte(`{"id":"10012"}`)
req, err := http.NewRequest("POST", "url", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/Text")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Println("responce Status:", resp.Status)
fmt.Println("responce Headers:", resp.Header)
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
fmt.Println("responce Body:", string(bodyText))
p := string(bodyText)
return p
}
I get the following error:
too many arguments to return, have (string), want ()
What does this error mean? How can I fix this?
Upvotes: 0
Views: 1206
Reputation: 46413
The error is exactly right. Your function signature is:
func Postfunc(w http.ResponseWriter , rep *http.Request)
It has no return values. Therefore, your last line:
return p
Has too many arguments, which would be any arguments at all. If you want to write text to the HTTP response, use the ResponseWriter
:
w.Write(bodyText)
Upvotes: 2