Search code examples
javascriptnode.jsencode

Encode a string for sending with HTTP request?


I am on Javascript/Node.js and when I'm making a HTTP request with this query parameter:

?key="https://me.yahoo.com/a/xt4hQ7QYssA8hymJKv8MeVQQKGhq_1jwvas-#a6e6f"

I get an error because it shops of everything after:

?key="https://me.yahoo.com/a/xt4hQ7QYssA8hymJKv8MeVQQKGhq_1jwvas-

I wonder how I can encode this string so it doesn't chop it off?


Solution

  • I'm assuming that the hash (#) at the end of your URL is actually part of the query argument. The problem is that Node.js is treating it as the hash of your overall URL, which plays no role in HTTP requests. Thus, you'll need to properly encode the query string.

    A structured API function like querystring.stringify is probably best.

    var query = querystring.stringify({
      key: '"https://me.yahoo.com/a/xt4hQ7QYssA8hymJKv8MeVQQKGhq_1jwvas-#a6e6f"'
    });