-2

I am using an HTTP client for Golang and the format of the POST request is as follows:

package main

import (
    "log"

    "github.com/monaco-io/request"
)
var name,pass string.  //assume name and pass hold a value calculated earlier
func main() {
    client := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Params: map[string]string{"random": "random"},
        **Body:   []byte(`{"username": "ABC", "password": "XYZ"}`)**,
    }
    resp, err := client.Do()

    log.Println(resp.Code, string(resp.Data), err)
}

I want to replace "ABC" by my variable name and "XYZ" by my variable pass.** How can I replace a constant string value by a variable which holds some string value calculated in a function within my code.

Kushagra
  • 1
  • 1
  • 1
    You're asking how to marshal JSON. There are countless examples on SO, and on the Internet. If the dupe question isn't sufficient, do a search for more examples. – Jonathan Hall Jan 18 '21 at 08:06

1 Answers1

-2

simply use fmt.Sprintf function

fmt.Sprintf(`{"username": "%s", "password": "%s"}`, name, pass)

https://golang.org/pkg/fmt/#Sprintf

full code will look like

package main

import (
    "fmt"
    "log"


    "github.com/monaco-io/request"  // must be v0.0.2
)

var name, pass string //assume name and pass hold a value calculated earlier

func main() {
    client := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Params: map[string]string{"random": "random"},
        Body:   []byte(fmt.Sprintf(`{"username": "%s", "password": "%s"}`, name, pass)),
    }
    resp, err := client.Do()

    log.Println(resp.Code, string(resp.Data), err)
}
XiaoXiao
  • 75
  • 2
  • 8
  • I tried the above function but I could not get it to work when I put it inside the byte array in the request body. Can you clarify the body format? I tried 2 different formats: `Body : []byte(fmt.Sprintf(`{"username": "%s", "password": "%s"}`, name, pass)) ` AND `Body : []byte(`fmt.Sprintf(`{"username": "%s", "password": "%s"}`, name, pass)`) ` but both of the above formats gave syntax error – Kushagra Jan 18 '21 at 06:11
  • 2
    What is your error response? Maybe we can check the version of package "monaco-io/request" in the go.mod file first. The package "monaco-io/request" has a lot of change in v1, and your use case can only be used in v0.0.2. – XiaoXiao Jan 18 '21 at 06:34
  • It says unexpected {, expecting expression when I use Body: []byte({fmt.Sprintf(`{"username":"&s"}`,name)}), – Kushagra Jan 18 '21 at 07:06
  • the format verbs is "%s" instead of "&s". there are more info https://golang.org/pkg/fmt/#hdr-Printing – XiaoXiao Jan 18 '21 at 07:10
  • I am sorry I used %s only, printed &s by mistake. – Kushagra Jan 18 '21 at 07:13
  • try to change `[]byte({ string })` => `[]byte( string )` – XiaoXiao Jan 18 '21 at 07:21
  • 2
    Never use `fmt.Sprintf` to create JSON. Do actual JSON marshaling instead! – Jonathan Hall Jan 18 '21 at 08:05
  • Future readers: please don't do this. – John Dibling Jan 19 '21 at 16:07