var Request = require("request")
var options = {
url: option.apiUrl,
qs: {
apiKey: "T%2BLkADWTX0OzgMMDmEPHffxQNYcgNMyfYJcXyR0cl0%2BNe8w0g%3D%3D",
}
json: true
}
function handleResponse (error, response, body) {
console.log(response) //apiKey is changed to T%252BLkADWTX0OzgMMDmEPHffxQNYcgNMyfYJcXyR0cl0%252BNe8w0g%253D%253D
}
Request(options, handleResponse);
嗨,
apiKey更改为错误的密钥。
我应该在请求qs上添加什么选项来禁用字符串编码?
或者什么是获取Json API的替代方法?
答案 0 :(得分:3)
因为你的api密钥是用encodeURIComponent()作为查询字符串进行uri编码的,所以当再次编码请求时,像%3D这样的特殊字符变为25%3D,如果使用decodeURIComponent,你可以拥有正确的apiKey编码正确
var string = "T%2BLkADWTX0OzgMMDmEPHffxQNYcgNMyfYJcXyR0cl0%2BNe8w0g%3D%3D"
decodeURIComponent(string) // "T+LkADWTX0OzgMMDmEPHffxQNYcgNMyfYJcXyR0cl0+Ne8w0g=="
PS:你不应该在SO
上共享API密钥