-
I am using The version that works (explicit map): package main
import (
"fmt"
"github.com/go-resty/resty/v2"
)
func sendToHass(sensor string, value string, attributes map[string]string) error {
resp, err := resty.New().R().
SetAuthToken("eyXXTU").
SetBody(map[string]interface{}{
"state": "hello",
"attributes": map[string]string{},
}).
Post(fmt.Sprintf("https://hass.example.com/api/states/sensor.dash_%v", sensor))
if (resp.StatusCode() == 200 || resp.StatusCode() == 201) && err == nil {
return nil
} else {
return fmt.Errorf("unexpected answer (not 200 or 201): %v (err: %v)", resp.StatusCode(), err)
}
} I then wanted to replace the package main
import (
"fmt"
"github.com/go-resty/resty/v2"
)
type State struct {
State string
// Attributes map[string]string
}
func sendToHass(sensor string, value string, attributes map[string]string) error {
resp, err := resty.New().R().
SetAuthToken("eyXXXXXU").
SetBody(State{
State: "hello",
// Attributes: attributes,
}).
Post(fmt.Sprintf("https://hass.example.com/api/states/sensor.dash_%v", sensor))
if (resp.StatusCode() == 200 || resp.StatusCode() == 201) && err == nil {
return nil
} else {
return fmt.Errorf("unexpected answer (not 200 or 201): %v (err: %v)", resp.StatusCode(), err)
}
} This returns a
Since this is the only change in the code there must be something strange with the body, which should be sent out as {
"state": "something",
"attributes": {}
} (with or without I would appreciate very much any pointers. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @wsw70, Disclaimer: this answer reference to my project experience before. I've try both of your code to using webhook.site. 🔭 ### Firstly, i check the using The body sent was: {
"attributes": {},
"state": "hello"
} 🔭 ### Secondly, i check the using The body sent was: {
"State": "hello"
} 🚀 ### Conclusion, if you want using struct to body json, use the Fix the code into like this: package main
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
)
// You have to add the struct tags on here
type State struct {
State string `json:"state"`
Attributes map[string]string `json:"attributes"`
}
func sendToHass(sensor string, value string, attributes map[string]string) error {
// Prepare your struct
state := State{
State: "hello",
Attributes: attributes,
}
// Convert struct to json byte
stateByte, _ := json.Marshal(state)
resp, err := resty.New().R().
SetAuthToken("eyXXXXXU").
SetBody(string(stateByte)). // Use in here. Also mapping type of byte to string
Post("https://webhook.site/9e0d8a64-0ed1-4e28-ae32-ddd26fae5e1d")
if (resp.StatusCode() == 200 || resp.StatusCode() == 201) && err == nil {
return nil
} else {
return fmt.Errorf("unexpected answer (not 200 or 201): %v (err: %v)", resp.StatusCode(), err)
}
}
func main() {
sendToHass("sensor", "value", map[string]string{})
} You can see more about struct tags on here: https://www.digitalocean.com/community/tutorials/how-to-use-struct-tags-in-go I hope this will help your discussions. |
Beta Was this translation helpful? Give feedback.
Hi @wsw70,
Disclaimer: this answer reference to my project experience before.
I've try both of your code to using webhook.site.
🔭 ### Firstly, i check the using
setBody(map[string]interface{})
, and the result was:The body sent was:
🔭 ### Secondly, i check the using
setBody(State{})
, and the result was:The body sent was:
🚀 ### Conclusion, if you want using struct to body json, use the
struct tags
and convert the struct to json.Fix the code into like this: