{"id":22295106,"url":"https://github.com/andrewburian/mediatype","last_synced_at":"2025-03-25T22:22:39.238Z","repository":{"id":57576799,"uuid":"103995500","full_name":"AndrewBurian/mediatype","owner":"AndrewBurian","description":"Content-Type helpers for HTTP content negotiation","archived":false,"fork":false,"pushed_at":"2018-11-27T07:04:14.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T19:43:23.395Z","etag":null,"topics":["content-type","golang","http"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AndrewBurian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-18T21:49:07.000Z","updated_at":"2018-11-27T07:03:04.000Z","dependencies_parsed_at":"2022-08-29T00:30:18.054Z","dependency_job_id":null,"html_url":"https://github.com/AndrewBurian/mediatype","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndrewBurian%2Fmediatype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndrewBurian%2Fmediatype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndrewBurian%2Fmediatype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndrewBurian%2Fmediatype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndrewBurian","download_url":"https://codeload.github.com/AndrewBurian/mediatype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245551085,"owners_count":20633971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["content-type","golang","http"],"created_at":"2024-12-03T17:40:30.501Z","updated_at":"2025-03-25T22:22:39.215Z","avatar_url":"https://github.com/AndrewBurian.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MediaType\nMediaType is a fully [RFC 7231](https://tools.ietf.org/html/rfc7231#section-5.3) compliant helper library for doing content-type negotiation.\n\n## Usage\nThe core of the library is a single `ContentType` object that represents a single full definition for a transfer encoding type like `application/json`. This can range from the extremely simple types like the wildcard `*/*` all the way up to very complex negotiation types like `text/html; charset=utf-8; q=0.1`.\n\n`ContentType`'s can be created directly from strings with `ParseSingle()`, or lists of them with `Parse()`. The list is more common since content negotiation may include more than one option.\n\nWhen performing negotiation, if your service only supports one type, you can check if it's supported by the client with `ContentList.SupportsType()`, which will check if your content type is supported at all by the client.\n\nIf you support multiple return types, you can choose the best type based on the clients request by the `ContentTypeList.PreferredMatch()` against a list of types your server supports. This will return a single `ContentType` from the options that should be used.\n\n## Example\n```go\nfunc HandleRequest(w http.ResponseWriter, r *http.Request) {\n\n\n    // Define what we support returning responses in\n    //\n    // in actual code you'd do this once at startup and not in the\n    // response handler each time\n    ExpectedRequestType, _ := mediatype.ParseSingle(\"application/json\")\n    SupportedResponseTypes, _ := mediatype.Parse(\"application/json, text/html\")\n\n    // Get the client supported types\n    reqType, resOptions, err := mediatype.ParseRequest(r)\n    if err != nil {\n        http.Error(...)\n        return\n    }\n\n    // check the request contains what we're expecting\n    if !reqType.Matches(ExpectedRequestType) {\n        http.Error(...)\n        return\n    }\n\n    // Determine if we can respond\n    resType := SupportedResponseTypes.PreferredMatch(resOptions)\n    if resType == nil {\n        http.Error(...)\n        return\n    }\n\n    // Do something\n    responseData := getData(r)\n\n    // Render response\n    if resType.SubType == \"json\" {\n        writeJson(w, responseData)\n        return\n    }\n\n    if resType.SubType == \"html\" {\n        writeHtml(w, responseData)\n        return\n    }\n\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewburian%2Fmediatype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrewburian%2Fmediatype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewburian%2Fmediatype/lists"}