我正在尝试创建一个带有一些要设置的表单参数的 postHTTP 请求。我正在使用带有 Node 服务器的 axios。我已经有一个构建 url 的 java 代码实现,如下所示:
JAVA 代码:
HttpPost post = new HttpPost(UriBuilder.fromUri (getProperty("authServerUrl"))
.path(TOKEN_ACCESS_PATH).build(getProperty("realm")));
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new NameValuePair("username",getProperty ("username")));
formParams.add(new NameValuePair("password",getProperty ("password")));
formParams.add(new NameValuePair("client_id, "user-client"));
我正在尝试在 axios 中做同样的事情。
AXIOS 实现:
axios.post(authServerUrl +token_access_path,
{
username: 'abcd', //gave the values directly for testing
password: '1235!',
client_id: 'user-client'
}).then(function(response) {
console.log(response); //no output rendered
}
在 post 请求中设置这些表单参数的方法是否正确?
最佳答案
您必须执行以下操作:
var querystring = require('querystring');
//...
axios.post(authServerUrl + token_access_path,
querystring.stringify({
username: 'abcd', //gave the values directly for testing
password: '1235!',
client_id: 'user-client'
}), {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(function(response) {
console.log(response);
});
关于javascript - Axios Http 客户端 - 如何使用表单参数构造 Http Post url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756756/