ajsie
ajsie

Reputation: 79796

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?

Upvotes: 0

Views: 8932

Answers (3)

yikekas
yikekas

Reputation: 147

Use encodeURIComponent as Husky mentioned in his comment.

?key=encodeURIComponent(https://me.yahoo.com/a/xt4hQ7QYssA8hymJKv8MeVQQKGhq_1jwvas-#a6e6f)

Upvotes: 0

ide
ide

Reputation: 20828

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"'
});

Upvotes: 8

Chris Cherry
Chris Cherry

Reputation: 28574

urlencode it.

in Javascript: escape(string)

Upvotes: 1

Related Questions