woocommerce / woocommerce-rest-api-js-lib

New JavaScript library for WooCommerce REST API
https://www.npmjs.com/package/@woocommerce/woocommerce-rest-api
MIT License
286 stars 75 forks source link

HPE_INVALID_HEADER_TOKEN - NodeJS v12 #75

Open gabrielgiorisatto opened 4 years ago

gabrielgiorisatto commented 4 years ago

All my requests using NodeJS v12 are returning Parse Error, and I think this is probably because of space in the header.

There are some discussions related here.

I had to downgrade my NodeJS to v10 and use the flag --insecure-http-parser to make it work.

climba03003 commented 4 years ago

As you can see the discussion, it is related to the core package brings by node. Nothing we can do here to solve the issue. My suggestion would be check which header cause the issue, then disable that plugins which generate the header.

gabrielgiorisatto commented 4 years ago

But the header is auto-generated from the Woocommerce rest API. Is it possible to change the header manually?

climba03003 commented 4 years ago

I do not think the invalid header is generated by this package.

For the header, you can change the header by passing axiosConfig to the constructor. Example:

import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";

const api = new WooCommerceRestApi({
  url: "http://example.com",
  consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  version: "wc/v3",
  axiosConfig: {
    header: {
        'content-type': 'application/json'
    }
  }
});
gabrielgiorisatto commented 4 years ago

Thank you for your help, but I removed this flag and used the axiosConfig that you recommended, and I'm still getting this error. Obs: I replaced some private infos with 'x'

The only way that it worked for me was using the --insecure-http-parser flag.

climba03003 commented 4 years ago

In this case, I think the invalid header is come from the response. As node v12 changed the behavior of http parser to be more strict than before, adding the flags seem to be the only options.