Skip to content

SetBody behaves differently depending on the method used #581

Closed Answered by kecci
wsw70 asked this question in Q&A
Discussion options

You must be logged in to vote

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:

{
  "attributes": {},
  "state": "hello"
}

🔭 ### Secondly, i check the using setBody(State{}), and the result was:

The body sent was:

{
  "State": "hello"
}

🚀 ### 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:

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

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@wsw70
Comment options

@kecci
Comment options

Answer selected by wsw70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants