Search code examples
lualuajit

How to access body in luasocket as binary data?


This code:

local client = require("socket.http")
local resp = client.request("some_server.com/some_image123.png")

How do I get the body -- as binary data / image -- of a response? Here "resp" isn't that.


Solution

  • You know, had you read the manual, you'd know this:

    If successful, the simple form returns the response body as a string, followed by the response status code

    I'll give you the benefit of the doubt and assume you really didn't know where to find that information, but even a quick google search for "luasocket reference" would have brought that page up.


    EDIT: Or maybe you already knew that, and instead didn't realize that a string in Lua is also a representation of binary data. Same as in any other language, Lua strings are just arrays of bytes under the hood, but other than, for example C, they can contain any byte, including 0x00, so you can use them to store any sort of binary data you want.