java - Java中Http post的NodeJS等效代码是什么
问题描述
我目前正在尝试将 java 项目转换为 Node.js(with Express.js) 项目。
这是一个在java中运行良好的请求,响应状态码是200 ok
HttpPost postFormRequest = new HttpPost(host + "/auth/j_security_check");
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("j_username", rtcLogin));
nvps.add(new BasicNameValuePair("j_password", rtcPassword));
postFormRequest.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse postFormResponse = null;
postFormResponse = client.execute(postFormRequest);
现在,当我在 Node.js 中这样做时:
const host = 'https://host.com/ccm';
const rtcAuthPath = '/auth/j_security_check';
const credentials = { j_username: 'login', j_password: '********' };
const authOptions = {
url: host + rtcAuthPath,
body: JSON.stringify(credentials)
};
function auth(error, response, body){
log(response);
}
request.post(authOptions, auth);
有了上面的代码,我得到了302 Found
我看到的唯一区别是java中的表单编码:
postFormRequest.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
那么在 Node.js 中 this 的等价物是什么?
解决方案
推荐阅读
- docusignapi - Docusign 上的 eventNotification 没有响应
- javascript - 添加由选择更改触发的 HTML 代码
- .net-core - .NET Standard 中的“绑定”类比是什么?我在哪里可以找到它?
- android-studio - android studio组件在设计活动时未显示
- multithreading - 多生产者多消费者队列中的队列覆盖
- java - JSON通过坐标获取地址
- reactjs - 只有在道具中满足某些条件时才添加 HoC
- python - 使用 scipy.signal.spectrogram 在 pyqtgraph 中绘制 wavfile 的频谱
- c# - 带有实体框架 6 的 MySql:子表的二进制(18)到字节 [] 转换失败
- json - 为什么这个 JSON 对象会针对这个 JSON 模式进行验证?