I want to pass a JSON object to a function in GOLANG ,so how would I define my parameters,would it be fine if I can define my params as string .below is a sample
func getDetailedNameSpace(authentication string,id string) string{
var jsonStr = []byte(string);
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, _ := http.NewRequest("PUT", "https://"+authentication.endPoint+"/object/namespaces/"+id, bytes.NewBuffer(jsonStr))
req.Header.Add("X-Sds-Auth-Token",authentication.authtoken)
req.Header.Add("Accept","application/json")
res,err:=client.Do(req)
body, err := ioutil.ReadAll(res.Body)
if err!=nil{
fmt.Printf("%s",err)
}
return string(body);
}
Also i have to validate the params ,as in python we can have like below
def getDetailedNameSpace(authentication,id=None):
assert authentication!=None,"Authentication Required"
I'm assuming you're trying to put the JSON as the body of your PUT
request. In this case you'll just want to use assignment. The request object has a field of type io.ReadCloser
for it.
req.Header.Add("Accept","application/json")
req.Body = bytes.NewBufferString(MyJsonAsAString)
res,err:=client.Do(req)
There are some other methods like http.Post
which take the body as an argument but in this case, the Do
method takes a Request
object as an argument and that has a property for the Body
.