使用HTTP请求编码字符串以进行发送? [英] Encode a string for sending with HTTP request?

查看:149
本文介绍了使用HTTP请求编码字符串以进行发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript / Node.js上,当我使用此查询参数发出HTTP请求时:

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得到一个错误,因为它之后的所有商店:

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?

推荐答案

我假设你URL的末尾的散列(#)实际上是查询参数的一部分。问题是Node.js将其视为整个URL的哈希值,它在HTTP请求中不起作用。因此,您需要正确编码查询字符串。

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.

结构化API函数,如 querystring.stringify 可能是最好的。

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

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

这篇关于使用HTTP请求编码字符串以进行发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆