Creating redirect with embedded json content

Hi all,

So, what i would like to do is generate a redirect url that is embedded with custom, user specific json content. Here is some background:

  • Im hosting a login portal using caddy-auth-port that has a list of my services that each redirect to a subdomain
  • I need to have one of the redirect links have custom json content embedded in the redirect that is based on the currently logged in user (the content is for an access token to bypass login on the service)
  • I dont know the best way to achieve this. Is this achievable from just a custom Caddyfile config or do I need to write a new module to take care of this?

Basically here is the sequence of events:

  • User logs in
  • User wants to goto service “ombi”
  • Caddy references currently logged in user, sends an http request to ombi with that users associated plex token, and receives back the auth_token string from ombi
  • Caddy then embeddeds the auth_token string from above into the request to ombi on the redirect

I think I can figure this out but just need some advice on which direction to head down. If i go the route of a http middleware module & write a ServeHttp function, how do I get if to only be used when the user clicks on the Ombi button from the portal page? Or do you have it filter out request that dont meet certain criteria (like request_destination = or something)?

Any help is appreciated.

I don’t understand — a redirect is usually just a response with a Location header telling the client where to go, usually no response body.

But if you need to respond with some JSON just do this:

respond `{"some": "json"}` 200

Basically what I need is, if a user were to click on one of my links from caddy-auth-portal, i need to something along these lines to happen:

  • caddy runs a command analogous to:
curl -v -H "Content-Type: application/json" -H "Accept: application/json" -d "{\"plexToken\":\"MY_TOKEN\"}"

then embed the response of that command into the redirect url before sending the client to that services (since the response contains the token necessary to bypass login).

You could make use of GitHub - abiosoft/caddy-exec: Caddy v2 module for running one-off commands maybe, but otherwise you’ll probably need to write your own handler plugin.


Im sorry to bother you, but ive been working on this and have hit a silly wall. For some reason I cannot get my module to set my variable correctly. I suspect something in the “unmarshal” function is messed up but im not sure. Can you take a look?

In the “sso.go” file is where the code is. The value I’m trying to get into the logger is “s.plex_token”. But the log files always show the string as being empty and I’m not sure why. I am calling my module like so:

route {
    plexsso {
        token "MY_TOKEN"

Any help is appreciated.

You need to use struct tags, i.e. the bits with json: on the line after your struct fields.

I think you also need to use PascalCase (i.e. first letter uppercase) for it to work. In Go, the concept of “public” properties is done via having the first letter uppercased. snake_case is atypical in Go.

Also I recommend using VSCode with the gopls language server to develop, it does a lot of heavy lifting to help you write Go code (automatic imports, great linting, etc) and don’t forget to use go fmt to format your code.


wow it ended up being the capitalization that was the issue. I never would have figured that out. Thank you so much!

1 Like

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