The data itself is stored in raw binary format on the blockchain, but it is published and retrieved via the API in hexadecimal format.
So you can convert it to hexadecimal before calling the publish API and then use gettxoutdata to retrieve the data in hexadecimal and convert it back to binary for retrieval.
Alternatively, you could consider using the binary cache which allows the binary data to be both published and retrieved via the file system – see the createbinarycache and txouttobinarycache commands, and the {"cache":"...."} form accepted by the publish API.