Expose RAW Response object
NOTE: not really an issue
i want to preform the following using httpie
const file = fs.createWriteStream("file.jpg");
const request = http.get("https://somedomain.com/sampleimage.jpg", function(response) {
response.pipe(file);
});
whats the best way of doing so?
double note: im asking here as it might be a good example to have for other users
Hey,
There's no access to the underlying response instance before its completion. I've thought about adding a httpie/raw or raw export to all modes that gives direct access to the response object (thus requiring you to do handle/parse/etc it). This might look something like this:
import { raw } from 'httpie';
async function download(file, writer) {
const resp = await raw('GET', file, ...);
return new Promise((res, rej) => {
resp.on('end', res).on('error', rej);
writer.pipe(resp);
});
}
The problem though is that I 100% cannot provide/guarantee any kind of uniformity. Unlike the current httpie@next exports, you would have to use raw specifically knowing if you're using the Fetch, XHR, or Node.js variant.
Put differently, the above snippet would (and could) only work in Node.js.
Where, any import { send } from 'httpie' statement will work regardless of the environment that httpie is running within
Ah i see i see.
okay thank you, this is very helpful. I'll use the built in http method as a replacement.
No problem. I'll keep this open for any feedback, but I definitely won't consider it a blocker/must-have for the next version release.
We ran into a related issue. We're talking to the authorize.net API, and even though its responses include the header content-type: application/json the response body contains a BOM, which is invalid, but authorize.net decided to leave it anyway.
So, we need to transform it or something before httpie does its JSON decoding and returns a rejection that doesn't include the body that would let us hack around it.
Including the raw response in the rejection would let us work around the authorize.net stupidity well enough