Setting headers to POST request

Hello everybody,

I want to create a http Post request. How can I set two header keys?
I tried this:

requestPost, _ := http.NewRequest("POST", "link", bytes.NewBuffer(jsonValue))
requestPost.Header.Set("Authorization", "key")
requestPost.Header.Set("Content-Type", "application/json")

but it doesn’t work.
Header.Set worked ok for GET where I had only one key, but how to set 2 keys?

  1. Always check errors.

requestPost, err := http.NewRequest(“POST”, “link”, bytes.NewBuffer(jsonValue))
if err != nil {
log.Fatal("Error reading request. ", err)

  1. I think you need to add the authorization type in the header
func basicAuth(username, password string) string {
  auth := username + ":" + password
   return base64.StdEncoding.EncodeToString([]byte(auth))

req.Header.Add("Authorization","Basic " + basicAuth("username1","password123"))

Thanks for the answer. I checked for errors, there aren’t any. For authentication, I don’t have username, all I need to set to my header is this:

Key                                      Value
Authorization                        "key"
Content-Type                         application/json



I made it work. All I had to do was:

requestPost.Header.Add("Content-Type", "application/json")

Thanks a lot!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.