开发者

Encode a string for sending with HTTP request?

开发者 https://www.devze.com 2023-02-13 06:17 出处:网络
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 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 ca开发者_如何转开发n encode this string so it doesn't chop it off?


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


urlencode it.

in Javascript: escape(string)


Use encodeURIComponent as Husky mentioned in his comment.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消