JSON Request / Response

Greetings!

Just wondering if it is possible to store key/value pairs in an external config.json file and be able to access those values to use within an HTTP header?

For example, if i have

{
“MyNamespace”:["/mynamespace"]
}

in an external file (which i can access now) and would like to use the MyNamespace value and set it in

req.Header.Set("X-Namespace", <use_MyNameSpaceHere>)

Any ideas if this would work? I’m struggling to figure this one out.

Thanks in advance!

Tyler

If your JSON config file holds the response header kv pairs in something like:


{
   ..,
   "responseheaders":{"Cache-Control":"no-store"},
   ...
}

that is read into a configuration struct with the following member:

type Context struct {
   ...
   ResponseHeaders  map[string]string
   ...
}
var Ctx Context

Then you can load the response header like this:

func myFileServer(w http.ResponseWriter, r *http.Request) {
   ...
    // load Header k:v pairs
   for k, v := range Ctx.ResponseHeaders {
      w.Header().Set(k, v)
   } 
   ...
}
1 Like

Thank you so very much!

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