-1
func send_msg(msg string, user string) {
    url := "https://test.com/"

    token := "Bearer " + get_token()

    header := req.Header{
        "Content-Type": "application/json",
        "Authorization": token,
    }

    // this string param can not use variable user and msg.
    // param := `{
    //  "email": "jjjj@gmail.com",
    //  "msg_type": "text",
    //  "content": { "text": "aaaaaaaaaaaaa" }
    // }`

    // this req.Param param error:missing type in composite literal
    param := req.Param{
        "email": user,
        "msg_type": "text",
        "content": {  "text" :  msg  }, 
    }
    r,_ := req.Post(url, header,  param)
    resp := r.String()
    log.Println(resp)

}

This req.Param param error:missing type in composite literal

This string param can not use variable user and msg.

How can I use variable param?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    Does this answer your question? [Missing type in composite literal](https://stackoverflow.com/questions/17912893/missing-type-in-composite-literal) – FuXiang Shu Dec 24 '19 at 01:01

1 Answers1

0

Assuming you are using "net/http", The Post function expects url, content-type and body in that order. Link : net/http#post Thus, for your particular case, it should look something like this

    url := "https://test.com/"
    contentType := "application/json"
    param := []byte(`{"email": user, "msg_type": "text","content": {  "text" :  "msg"  }, }`)
    resp, err := http.Post(url, contentType, bytes.NewBuffer(param))
    defer resp.Body.Close()

    if err != nil {
        //handle error
    } else {

        body, _ := ioutil.ReadAll(resp.Body)
        log.Println(body)
    }

If you want to add custom headers, you may use newRequest which gives you req and then you can add or set custom headers .

req.Headers.Set(existingHeaderWithNewValue)
req.Headers.Add(customHeader)

Ankit Shah
  • 11
  • 3