httpie icon indicating copy to clipboard operation
httpie copied to clipboard

Expose RAW Response object

Open jlvasquezcollado opened this issue 5 years ago • 5 comments

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

jlvasquezcollado avatar May 22 '20 11:05 jlvasquezcollado

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

lukeed avatar May 22 '20 15:05 lukeed

Ah i see i see.

okay thank you, this is very helpful. I'll use the built in http method as a replacement.

jlvasquezcollado avatar May 22 '20 16:05 jlvasquezcollado

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.

lukeed avatar May 22 '20 16:05 lukeed

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.

TehShrike avatar Jan 26 '22 00:01 TehShrike

Including the raw response in the rejection would let us work around the authorize.net stupidity well enough

TehShrike avatar Feb 12 '22 17:02 TehShrike