{"hash":"0xF5C6330C","ns":"CFX","name":"SET_HTTP_HANDLER","slug":"SET_HTTP_HANDLER","official":true,"named":true,"luaName":"SetHttpHandler","game":"cfx","apiset":"server","returnType":"void","params":[{"name":"handler","type":"func","description":"The handler function."}],"summary":"Sets the handler for HTTP requests made to the executing resource.","descriptions":[{"source":"cfx","format":"md","body":"Sets the handler for HTTP requests made to the executing resource.\n\nExample request URL: `http://localhost:30120/http-test/ping` - this request will be sent to the `http-test` resource with the `/ping` path.\n\nThe handler function assumes the following signature:\n\n```ts\nfunction HttpHandler(\n  request: {\n    address: string;\n    headers: Record<string, string>;\n    method: string;\n    path: string;\n    setDataHandler(handler: (data: string) => void): void;\n    setDataHandler(handler: (data: ArrayBuffer) => void, binary: 'binary'): void;\n    setCancelHandler(handler: () => void): void;\n  },\n  response: {\n    writeHead(code: number, headers?: Record<string, string | string[]>): void;\n    write(data: string): void;\n    send(data?: string): void;\n  }\n): void;\n```\n\n*   **request**: The request object.\n    *   **address**: The IP address of the request sender.\n    *   **path**: The path to where the request was sent.\n    *   **headers**: The headers sent with the request.\n    *   **method**: The request method.\n    *   **setDataHandler**: Sets the handler for when a data body is passed with the request. Additionally you can pass the `'binary'` argument to receive a `BufferArray` in JavaScript or `System.Byte[]` in C# (has no effect in Lua).\n    *   **setCancelHandler**: Sets the handler for when the request is cancelled.\n*   **response**: An object to control the response.\n    *   **writeHead**: Sets the status code & headers of the response. Can be only called once and won't work if called after running other response functions.\n    *   **write**: Writes to the response body without sending it. Can be called multiple times.\n    *   **send**: Writes to the response body and then sends it along with the status code & headers, finishing the request."}],"examples":[{"lang":"lua","code":"SetHttpHandler(function(request, response)\n  if request.method == 'GET' and request.path == '/ping' then -- if a GET request was sent to the `/ping` path\n      response.writeHead(200, { ['Content-Type'] = 'text/plain' }) -- set the response status code to `200 OK` and the body content type to plain text\n      response.send('pong') -- respond to the request with `pong`\n  else -- otherwise\n      response.writeHead(404) -- set the response status code to `404 Not Found`\n      response.send() -- respond to the request with no data\n  end\nend)","source":"cfx"}],"aliases":[],"enums":[],"related":[],"sources":["cfx"],"doc":3,"url":"https://natives.borderlinerp.com/natives/CFX/SET_HTTP_HANDLER","markdownUrl":"https://natives.borderlinerp.com/natives/CFX/SET_HTTP_HANDLER.md","snippets":{"lua":"SetHttpHandler(handler)","luaInvoke":"Citizen.InvokeNative(0xf5c6330c, handler)","js":"SetHttpHandler(handler);","jsInvoke":"Citizen.invokeNative('0xf5c6330c', handler);","csharp":"Function.Call((Hash)0xf5c6330c, handler);","notes":[]}}