{"id":15722648,"url":"https://github.com/selimanac/defold-tiny-http","last_synced_at":"2025-05-13T10:40:42.374Z","repository":{"id":152790549,"uuid":"217082704","full_name":"selimanac/defold-tiny-http","owner":"selimanac","description":"Simple http server and client for Defold","archived":false,"fork":false,"pushed_at":"2019-10-29T08:39:10.000Z","size":3918,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-20T23:32:33.375Z","etag":null,"topics":["defold","defold-game-engine","defold-library","http","http-client","http-server","server"],"latest_commit_sha":null,"homepage":"https://selimanac.github.io/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/selimanac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-23T14:49:19.000Z","updated_at":"2025-04-14T06:11:19.000Z","dependencies_parsed_at":"2023-07-01T19:15:34.073Z","dependency_job_id":null,"html_url":"https://github.com/selimanac/defold-tiny-http","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/selimanac%2Fdefold-tiny-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selimanac%2Fdefold-tiny-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selimanac%2Fdefold-tiny-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selimanac%2Fdefold-tiny-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/selimanac","download_url":"https://codeload.github.com/selimanac/defold-tiny-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253924765,"owners_count":21985172,"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":["defold","defold-game-engine","defold-library","http","http-client","http-server","server"],"created_at":"2024-10-03T22:08:46.189Z","updated_at":"2025-05-13T10:40:42.340Z","avatar_url":"https://github.com/selimanac.png","language":"C++","readme":"![Tiny Http Native Extension](https://selimanac.github.io/assets/gfx/tiny_http_dark.png)\n\nTiny Http - Defold Native Extension is a simple http server and client. \n\nAll requests and responses are JSON. You may consider of using [CJSON](https://github.com/Melsoft-Games/defold-cjson) for encoding and decoding.\n\nAll server responses for GET requests are static echo. POST responses are limited but customizable by using [`dhttp.server_post_content`](#dhttpserver_post_contentjson).\n\nServer works on iOS, Android, MacOS, Win 10 and Linux(Debian 10.x). HTML5 build is not supported. Since I don't need it, SSL not supported. But it is [possible](https://github.com/yhirose/cpp-httplib#openssl-support) to add this feature, feel free to PR if you want to. \n\n## Installation\nYou can use Tiny Http in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the dependencies field under project add:\n\n\thttps://github.com/selimanac/defold-tiny-http/archive/master.zip\n\t\n---\n\n## Examples\n\nDetailed server and client examples can be found in [examples](https://github.com/selimanac/defold-tiny-http/tree/master/examples) folder.\n\n### Server\n\n```lua\n-- Server settings\nlocal host = \"localhost\"\nlocal port = 8800\n\n-- Server callbacks\nlocal function server_callbacks(self, message)\n\t-- Decode response\n    local jresult = json.decode(message.result)\n    local event_id = message.event_id\n\n    if jresult.server_status == dhttp.SERVER_START then\n        -- Server started\n    elseif jresult.server_status == dhttp.SERVER_STOP then\n        -- Server stopped\n    else\n    \tif event_id == 123 then\n        \tpprint(jresult)\n        \t-- Do something fancy\n       end\n    end\nend\n\nfunction init(self)\n    -- Start the server\n    dhttp.server_start(host, port, server_callbacks)\nend\n```\n\n### Client\n```lua\n-- Client settings\nlocal host = \"localhost\"\nlocal port = 8800\n\n-- Client callbacks\nlocal function client_callbacks(self, message)\n    -- Decode response\n    local jresult = json.decode(message.result)\n\n    -- Check error\n    if jresult.error then\n        print(\"Error: \", jresult.error)\n        return\n    end\n\n    pprint(jresult)\nend\n\nfunction init(self)\n    -- Init the client\n    dhttp.client_start(host, port, client_callbacks)\n\n    -- Get endpoint\n    dhttp.client_get(\"/hi\", 123)\nend\n```\n\n\n## Server API\n\n#### dhttp.server_start(`host, port, callback, [log], [error], [endpoints]`)\n\n\nInit and start the server.\n\n| Param  | Desc |\n| ------------- | ------------- |\n| `host`  | (_string_) Host address or IP  |\n| `port`  | (_int_) Port number  |\n| `callback`  | (_function_) Callback function  |\n| `[log]`  | (_boolean_) Turn logging on/off. Default is false |\n| `[error]`  | (_boolean_) Turn error responses on/off. Default is true |\n| `[endpoints]`  | (_table_) Optional endpoints |\n\n#### dhttp.server_stop()\n\nGracefully shutdown the server.\n\n#### dhttp.is_server_running()\n\nCheck if server is running. Returns boolean.\n\n#### dhttp.server_post_content(`content`)\n\nSets the POST response for all POST endpoints. You can set this anytime.\n\n| Param  | Desc |\n| ------------- | ------------- |\n| `content `  | (string) JSON formated string  |\n\n\n### Endpoints\n\nThere are several built-in endpoints.\n\n| Endpoint  | Desc |\n| ------------- | ------------- |\n| \"/hi\" | Says hi!  |\n| \"/num/(\\d+)\" | (_GET_) Gets only [0-9] as integer  |\n| \"/str/(\\w+)\" | (_GET_) Gets only [a-zA-Z0-9] as string  |\n| \"/post\" | (_POST_) Generic post endpoint with params  |\n| \"/stop\" | (_GET_) Stop the server  |\n\nYou can define custom endpoints.  \nEndpoints support regex. But \"?\" character is reserved and may cause a crash.\n\n\n```lua\nlocal endpoints = {\n    {\n        endpoint_type = dhttp.METHOD_GET,\n        endpoint = \"/monster/(\\\\d+)\"\n    },\n    {\n        endpoint_type = dhttp.METHOD_POST,\n        endpoint = \"/move\"\n    }\n}\n\ndhttp.server_start(\"localhost\", 8888, server_callbacks, false, true, endpoints)\n\n```\n\n## Client API\n\n#### dhttp.client_start(`host, port, client_callbacks`)\n\nInit the client.\n\n| Param  | Desc |\n| ------------- | ------------- |\n| `host`  | (_string_) Host address or IP  |\n| `port`  | (_int_) Port number  |\n| `callback`  | (_function_) Callback function  |\n\n\n#### dhttp.client_hi()\n\nDefold says hi!\n\n#### dhttp.client_get(`endpoint, [event_id]`)\n\n| Param  | Desc |\n| ------------- | ------------- |\n| `endpoint` | (_string_) Endpoint address  |\n| `[event_id]`| (_int_) Event ID for tracking the action  |\n\nEvent IDs are for tracking the requests on server and client. They send as a header. You can easily group and parse your triggers by using event ids.\n\n#### dhttp.client_post(`endpoint, params, [event_id]`)\n\n| Param  | Desc |\n| ------------- | ------------- |\n| `endpoint` | (_string_) Endpoint address  |\n| `params` | (_string_) JSON formated string  |\n| `[event_id]`| (_int_) Event ID for tracking the action  |\n\nEvent IDs are for tracking the requests on server and client. They send as a header. You can easily group and parse your triggers by using event ids.\n\n```lua\nlocal temp_table = {\n        x = 10,\n        y = 20\n    }\n\n    jresult = cjson.encode(temp_table)\n\n    local params = {\n        position = jresult\n    }\n\n    dhttp.client_post(\"/post\", params, 1)\n```\n\n\n#### Constants\n\n##### dhttp.METHOD_GET\n##### dhttp.METHOD_POST\n##### dhttp.SERVER_START\n##### dhttp.SERVER_STOP\n\n## Dependencies\n\nBuild by using [cpp-httplib](https://github.com/yhirose/cpp-httplib)  \nCharacters by [@bevouliin](https://twitter.com/bevouliin)","funding_links":[],"categories":["Libraries"],"sub_categories":["Programming Language"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselimanac%2Fdefold-tiny-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fselimanac%2Fdefold-tiny-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselimanac%2Fdefold-tiny-http/lists"}