Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
HTTP Negotiation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Network
HTTP Negotiation
Compare revisions
v1.0.3 to v1.1.0
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
oss/libraries/go/network/http-negotiation
Select target project
No results found
v1.1.0
Select Git revision
Branches
master
Tags
v1.0.0
v1.0.1
v1.0.2
v1.0.3
v1.1.0
v1.2.0
v1.2.1
v1.2.2
v1.2.3
v1.2.4
v1.2.5
v1.3.0
v1.3.1
v1.3.2
v1.3.3
v1.3.4
17 results
Swap
Target
oss/libraries/go/network/http-negotiation
Select target project
oss/libraries/go/network/http-negotiation
1 result
v1.0.3
Select Git revision
Branches
master
Tags
v1.0.0
v1.0.1
v1.0.2
v1.0.3
v1.1.0
v1.2.0
v1.2.1
v1.2.2
v1.2.3
v1.2.4
v1.2.5
v1.3.0
v1.3.1
v1.3.2
v1.3.3
v1.3.4
17 results
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
new ContentType function
· 59f7404d
Volker Schukai
authored
2 years ago
Verified
59f7404d
Bump version to 1.1.0
· fd14cc40
Volker Schukai
authored
2 years ago
Verified
fd14cc40
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+32
-1
32 additions, 1 deletion
README.md
api.go
+5
-0
5 additions, 0 deletions
api.go
api_test.go
+18
-0
18 additions, 0 deletions
api_test.go
header.go
+1
-0
1 addition, 0 deletions
header.go
release.json
+1
-1
1 addition, 1 deletion
release.json
with
57 additions
and
2 deletions
README.md
View file @
fd14cc40
...
...
@@ -46,6 +46,28 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
```
### Accept Content Type
With the
`ContentType`
function you can negotiate the content type of HTTP request.
```
go
package
main
import
(
"net/http"
"gitlab.schukai.com/oss/libraries/go/network/http-negotiation"
)
func
handleRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
n
:=
negotiation
.
New
(
r
.
Header
)
if
n
.
ContentType
(
"application/json"
)
!=
""
{
// ...
}
}
```
### Language Negotiation
With the
`Language`
function you can negotiate the language of HTTP request.
...
...
@@ -66,7 +88,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
}
}
```
### Charset Negotiation
...
...
@@ -115,6 +136,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
```
## Changelog
### 1.1.0
*
Add
`ContentType`
function
### 1.0.0
*
Initial release
## Contributing
Merge requests are welcome. For major changes, please open an issue first to discuss what
...
...
This diff is collapsed.
Click to expand it.
api.go
View file @
fd14cc40
...
...
@@ -14,6 +14,11 @@ func New(header http.Header) *Negotiation {
return
&
Negotiation
{
header
}
}
func
(
n
*
Negotiation
)
ContentType
(
acceptance
...
string
)
string
{
parser
:=
newHeaderParser
(
n
.
header
,
false
)
return
parser
.
selectOffer
(
acceptance
,
parser
.
parse
(
headerContentType
))
}
// Type returns the most preferred content type from the HTTP Accept header.
// If nothing accepted, then empty string is returned.
func
(
n
*
Negotiation
)
Type
(
offers
...
string
)
(
bestOffer
string
)
{
...
...
This diff is collapsed.
Click to expand it.
api_test.go
View file @
fd14cc40
...
...
@@ -5,6 +5,24 @@ import (
"testing"
)
func
TestContentType
(
t
*
testing
.
T
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
n
:=
New
(
req
.
Header
)
if
n
.
ContentType
(
"application/json"
)
!=
"application/json"
{
t
.
Errorf
(
"handler returned wrong status code: got %v want %v"
,
n
.
Type
(
"application/json"
),
"application/json"
)
}
}
func
TestNegotiationLanguage
(
t
*
testing
.
T
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
...
...
This diff is collapsed.
Click to expand it.
header.go
View file @
fd14cc40
...
...
@@ -5,4 +5,5 @@ const (
headerAcceptLanguage
=
"Accept-Language"
headerAcceptEncoding
=
"Accept-Encoding"
headerAcceptCharset
=
"Accept-Charset"
headerContentType
=
"Content-Type"
)
This diff is collapsed.
Click to expand it.
release.json
View file @
fd14cc40
{
"version"
:
"1.
0.3
"
}
{
"version"
:
"1.
1.0
"
}
This diff is collapsed.
Click to expand it.