request / request-promise

The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.
ISC License
4.76k stars 297 forks source link

Non recoverable error when using a post-request with json data without 'json: true' #325

Open maze-le opened 5 years ago

maze-le commented 5 years ago

The library seems to trigger a non-catchable, non-recoverable error when using a post-request with a json-body without the option json: true.

    try {
      const url: string = "http://example.com/"
      const body: any = {"some": "json"};
      const postReturn = await post(url, { encoding: "utf8", body });

      return postReturn;
    } catch (error) {
      console.log("I cannot catch this", error);
    }

I am not sure if this is the fault of the library or an underlying problem with the nodejs runtime. I am using the library with typescript and the package @types/request-promise

lindenmckenzie commented 5 years ago

That body variable isn't JSON - should be '{ "some": "json" }'

e.g. (in Javascript, not Typescript):

const rp = require('request-promise');

const test = async () => {
  try {
    const url = 'http://example.com/';
    const body = '{ "some": "json" }';
    const postReturn = await rp(url, { encoding: 'utf8', body });
    return postReturn;
  } catch (error) {
    console.log('I cannot catch this', error);
  }
};

test();