My issue:
I need to extract the binary (01001) or whatever String I can extract from a file and send it via HTTP as my response (could be in a JSON or an XML, I don't care). I believe I am able to extract the contents of the file and send it to the client, but for some reason after my client writes it into its file system it can't be correctly executed and the contents of the file (in my case an mp3) used (in my case listened by the human user).
Diagram explaining what I am doing:
Easy to replicate:
The answer you give can be in any language, if you ask for what I am using, it is Rust for the server and Swift for the client, but because those languages are not as widely used as say JS, I am cool with any language for the answer as this question is more about the concept and implementation in general than implementation in a specific device/language.
I have replicated a little express server you can copy and paste in your machine, but you can get the mp3 file through your language of choice with this link https://download.samplelib.com/mp3/sample-3s.mp3 or any other link that sends back an mp3 file... the one I am actually using requires private keys but you can use any other, preferably a public one like the one above so that you can share it with me, but if you do not wanna do that, that is fine.
Sample Express JS Server (Does not matter what language you use for your answer):
app.js:
const express = require('express')
const app = express()
const port = 3000
const fetch = require("./fetch");
app.get('/', async (req, res) => {
let audioFile = await fetch.fetch("https://download.samplelib.com/mp3/sample-3s.mp3")
console.log(audioFile.body)
res.send(audioFile.body["_readableState"]["buffer"]["head"])
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
fetch.js:
exports.fetch = async function (url, init) {
const {default: fetch} = await import("node-fetch");
return await fetch(url, init);
};
File structure for this simple specific replication:
MODERATORS: Even if you se JavaScript in this post, it does not mean it has anything to do with it, it is used as an example and this question is language-agnostic. Please do not add any language tags in the post just because you saw JS in it, thank you.
Not Important At All Note (SKIP IT):
Each 1 and 0 is still one byte in memory, so if instead of getting the binary from the mp3 another string is extracted from it, like hex or whatever that takes up less bytes that the long full binary string in the HTTP response, would definitely be better. So if you want to add a RECOMMENDATION in that, you are welcome to do so.