Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gamemaker1/http-negotiator
A HTTP content negotiator for V
https://github.com/gamemaker1/http-negotiator
content-negotiation http v v-module vlang vlang-module
Last synced: 9 days ago
JSON representation
A HTTP content negotiator for V
- Host: GitHub
- URL: https://github.com/gamemaker1/http-negotiator
- Owner: gamemaker1
- License: isc
- Created: 2021-08-11T12:55:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-13T13:54:29.000Z (over 3 years ago)
- Last Synced: 2024-12-08T17:44:40.459Z (14 days ago)
- Topics: content-negotiation, http, v, v-module, vlang, vlang-module
- Language: V
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# HTTP Negotiator
A HTTP content negotiator for V that allows you to parse the `Accept` header of an HTTP request and get the preferred response media type specified by the client.
## Usage/Examples
First, download the library:
```
v install --git https://github.com/gamemaker1/http-negotiator
```Then use it:
```v
// Import the library
import gamemaker1.http_negotiator// You need to get this value from the request object
// In this case, since it is an example, this value is hard-coded
// This value in an `Accept` HTTP header means that the client would like a
// response:
// - in the `application/json` format first
// - if that's not available, give it in the `text/plain` format
// - if that's not available, we're fine with anything
// The `q` paramter stands for `quality`, which indicates how much the client
// favours that response media type (0 is least, 1 is most; default is 1).
accept_header_value := 'application/json;charset=utf-8, text/plain;q=0.9;charset=utf-8, text/html;q=0.8;charset=utf-8, */*;q=0.7'
// Which media types you (the server) can respond in
possible_media_types := ['text/html', 'application/json']// Now the library allows you to get:
// 1. a list of response media types in order of client's preference
// 2. the most preferred media type that the client wants// For 1:
preferred_media_types := http_negotiator.get_media_types(
// The `Accept` header passed by the client
accept_header_value,
// Valid media types that you can respond in
possible_media_types
) or {
// If:
// - none of the client's accepted media types can be provided by the server
// - a media type provided by the client is an invalid one
// Then: use 'application/xml' by default
'application/xml'
} // Returns ['application/json', 'text/html']// For 2:
preferred_media_type := http_negotiator.get_media_type(
// The `Accept` header passed by the client
accept_header_value,
// Valid media types that you can respond in
possible_media_types
) or {
// If:
// - none of the client's accepted media types can be provided by the server
// - a media type provided by the client is an invalid one
// Then: use 'application/xml' by default
'application/xml'
} // Returns 'application/json'
```An example with VWeb can be found in [`examples/vweb.v`](/examples/vweb.v)
## Issues/Contributing
Thank you for using this library. Please feel free to open an issue to report a bug or suggest something. PRs are also welcome!
## License
This project is licensed under the ISC license. View the [license file](/license.md) for more details.
Copyright 2021 Vedant K (gamemaker1) \