// https://developer.fastly.com/solutions/examples/manipulate-query-string
package main
import (
"context"
"fmt"
"io"
"github.com/fastly/compute-sdk-go/fsthttp"
)
// BackendName is the name of our service backend.
const BackendName = "origin_0"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
// This requires your service to be configured with a backend
// named "origin_0" and pointing to "https://http-me.glitch.me".
// Use Query() method to store query string parameters as an url.Values map.
values := r.URL.Query()
// Add query string parameter.
values.Add("ccc", "new")
// Remove any Google Analytics campaign parameters.
values.Del("utm_campaign")
values.Del("utm_source")
// Use Encode() method to transform url.Values map into a URL-encoded string.
// This sorts the query string parameters in alphabetical order by default.
r.URL.RawQuery = values.Encode()
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}