Skip to content

Instantly share code, notes, and snippets.

@sedkis
Created May 6, 2024 17:59
Show Gist options
  • Save sedkis/09894e153835068eb0a4bc62f931c7a1 to your computer and use it in GitHub Desktop.
Save sedkis/09894e153835068eb0a4bc62f931c7a1 to your computer and use it in GitHub Desktop.
// TerminateRequest checks for a specific query parameter and terminates the request if present.
func TerminateRequest(rw http.ResponseWriter, r *http.Request) {
// Check for the query parameter
if value := r.URL.Query().Get("terminate"); value == "true" {
// Prepare the data to send in response
responseData := map[string]string{
"message": "Request terminated by custom Tyk plugin.",
}
jsonData, err := json.Marshal(responseData)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
// Set the content type and write the JSON response
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
rw.Write(jsonData)
// Terminate the request processing
return
}
// If the parameter is not present, do nothing and let the request continue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment