-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1327 from gofr-dev/release/1.29.0
Release/1.29.0
- Loading branch information
Showing
40 changed files
with
733 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 95 additions & 33 deletions
128
docs/advanced-guide/setting-custom-response-headers/page.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,118 @@ | ||
# Setting Custom Response Headers | ||
# Custom Response Headers and Metadata in GoFr | ||
|
||
In GoFr, you can customize HTTP response headers using the `Response` struct, allowing you to add extra information to | ||
responses sent from your application. This feature can be useful for adding metadata, such as custom headers, security | ||
policies, or other contextual information, to improve the client-server communication. | ||
GoFr simplifies the process of adding custom HTTP response headers and metadata to API responses using the `Response` struct. This feature allows you to include additional information such as custom headers or metadata to enhance client-server communication while keeping your data payload clean and structured. | ||
|
||
## Using the Response Struct | ||
## Features | ||
|
||
To use custom headers in your handler, create and return a Response object within the handler function. This object | ||
should contain the response data along with a Headers map for any custom headers you wish to add. | ||
1. **Custom Headers**: Add key-value pairs for headers, useful for: | ||
- Security policies | ||
- Debugging information | ||
- Versioning details | ||
|
||
### Example: | ||
**Type**: `map[string]string` | ||
- Keys and values must be strings. | ||
|
||
Below is an example showing how to use the Response struct in a GoFr handler. In this case, the `HelloHandler` function | ||
returns a greeting message along with two custom headers: X-Custom-Header and X-Another-Header. | ||
2. **Metadata**: Include optional contextual information like: | ||
- Deployment environment | ||
- Request-specific details (e.g., timestamps, tracing IDs) | ||
|
||
**Type**: `map[string]any` | ||
- Keys must be strings, and values can be of any type. | ||
|
||
When metadata is included, the response structure is: | ||
```json | ||
{ | ||
"data": {}, | ||
"metadata": {} | ||
} | ||
``` | ||
|
||
If metadata is omitted, the response defaults to: | ||
|
||
```json | ||
{ | ||
"data": {} | ||
} | ||
``` | ||
|
||
### Example Usage | ||
|
||
#### Adding Custom Headers and Metadata | ||
To include custom headers and metadata in your response, populate the Headers and MetaData fields of the Response struct in your handler function. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"gofr.dev/pkg/gofr" | ||
"gofr.dev/pkg/gofr/http/response" | ||
"time" | ||
|
||
"gofr.dev/pkg/gofr" | ||
"gofr.dev/pkg/gofr/http/response" | ||
) | ||
|
||
func main() { | ||
// Create a new application | ||
a := gofr.New() | ||
app := gofr.New() | ||
|
||
// Add the route | ||
a.GET("/hello", HelloHandler) | ||
app.GET("/hello", HelloHandler) | ||
|
||
// Run the application | ||
a.Run() | ||
app.Run() | ||
} | ||
|
||
func HelloHandler(c *gofr.Context) (interface{}, error) { | ||
name := c.Param("name") | ||
if name == "" { | ||
c.Log("Name came empty") | ||
name = "World" | ||
} | ||
|
||
headers := map[string]string{ | ||
"X-Custom-Header": "CustomValue", | ||
"X-Another-Header": "AnotherValue", | ||
} | ||
|
||
return response.Response{ | ||
Data: "Hello World from new Server", | ||
Headers: headers, | ||
}, nil | ||
name := c.Param("name") | ||
if name == "" { | ||
c.Log("Name parameter is empty, defaulting to 'World'") | ||
name = "World" | ||
} | ||
|
||
// Define custom headers (map[string]string) | ||
headers := map[string]string{ | ||
"X-Custom-Header": "CustomValue", | ||
"X-Another-Header": "AnotherValue", | ||
} | ||
|
||
// Define metadata (map[string]any) | ||
metaData := map[string]any{ | ||
"environment": "staging", | ||
"timestamp": time.Now(), | ||
} | ||
|
||
// Return response with custom headers and metadata | ||
return response.Response{ | ||
Data: map[string]string{"message": "Hello, " + name + "!"}, | ||
Metadata: metaData, | ||
Headers: headers, | ||
}, nil | ||
} | ||
``` | ||
|
||
### Example Responses | ||
#### Response with Metadata: | ||
When metadata is included, the response contains the metadata field: | ||
|
||
```json | ||
{ | ||
"data": { | ||
"message": "Hello, World!" | ||
}, | ||
"metadata": { | ||
"environment": "staging", | ||
"timestamp": "2024-12-23T12:34:56Z" | ||
} | ||
} | ||
``` | ||
|
||
#### Response without Metadata: | ||
If no metadata is provided, the response only includes the data field: | ||
|
||
```json | ||
{ | ||
"data": { | ||
"message": "Hello, World!" | ||
} | ||
} | ||
``` | ||
|
||
|
||
This functionality offers a convenient, structured way to include additional response information without altering the | ||
core data payload. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.