Task - Manage the format requested by the client
Created by: benoitdm-oslandia
In GitLab by @nrevelant on Oct 13, 2022, 16:43
Currently, pgfeatureserve seems to let the client choose the responses format on GET requests. :
- through route suffix, like file extension (net.go) :
func RequestedFormat(r *http.Request) string {
// first check explicit path
path := r.URL.EscapedPath()
if strings.HasSuffix(path, ".html") {
return FormatHTML
}
if strings.HasSuffix(path, ".json") {
return FormatJSON
}
if strings.HasSuffix(path, ".txt") {
return FormatText
}
if strings.HasSuffix(path, ".svg") {
return FormatSVG
}
- through the "Accept" header
// Use Accept header if present
hdrAccept := r.Header.Get("Accept")
//fmt.Println("Accept:" + hdrAccept)
if strings.Contains(hdrAccept, ContentTypeSchemaJSON) {
return FormatSchemaJSON
}
if strings.Contains(hdrAccept, ContentTypeHTML) {
return FormatHTML
}
return FormatJSON
However this parameter seems to have a limited support with mostly JSON formatted response ( handleItem() function inside handler.go ) :
switch format {
case api.FormatJSON:
return writeItemJSON(ctx, w, name, fid, param, urlBase, crs)
case api.FormatHTML:
return writeItemHTML(w, tbl, name, fid, query, urlBase)
default:
return nil
}
The TestWeakEtagFromDifferentRepresentationsDb test for example will cover this use case.
NB: In the case of an unsupported format, the return code should be : 406 Not Acceptable