Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Unnecessary JSON.stringify #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

notmike101
Copy link

Axios "automagically" stringfies the post body, there's no need to pre-stringify.

Pre-stringifying can result in unexpected behavior when you're passing in JSON content that includes quotes, such as { name: "My name is \"Mike\"" }

Axios "automagically" stringfies the post body, there's no need to pre-stringify.

Pre-stringifying can result in unexpected behavior when you're passing in JSON content that includes quotes, such as `{ name: "My name is \"Mike\"" }`
@notmike101
Copy link
Author

For anyone who lands here from a search engine looking for a fix and doesn't want to install my fork, you can patch this yourself in your own code by doing the following:

(This assumes you're initializing WooCommerceRestAPI and storing it in a variable named api, just like the README example)

api._request = function(method, endpoint, data, params = {}) {
  const url = this._getUrl(endpoint, params);

  const headers = {
    Accept: "application/json"
  };
  // only set "User-Agent" in node environment
  // the checking method is identical to upstream axios
  if (
    typeof process !== "undefined" &&
    Object.prototype.toString.call(process) === "[object process]"
  ) {
    headers["User-Agent"] =
      "WooCommerce REST API - JS Client/" + this.classVersion;
  }

  let options = {
    url: url,
    method: method,
    responseEncoding: this.encoding,
    timeout: this.timeout,
    responseType: "json",
    headers
  };

  if (this.isHttps) {
    if (this.queryStringAuth) {
      options.params = {
        consumer_key: this.consumerKey,
        consumer_secret: this.consumerSecret
      };
    } else {
      options.auth = {
        username: this.consumerKey,
        password: this.consumerSecret
      };
    }

    options.params = { ...options.params, ...params };
  } else {
    options.params = this._getOAuth().authorize({
      url: url,
      method: method
    });
  }

  if (data) {
    options.headers["Content-Type"] = "application/json;charset=utf-8";
    // WHY THE FUCK DID THEY DO THIS?
    // options.data = JSON.stringify(data);
  }

  // Allow set and override Axios options.
  options = { ...options, ...this.axiosConfig };

  return axios(options);
}

After you implement this patch, you use this library as normal but will no longer run into errors because of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant