首页 > 解决方案 > 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 的等价物是什么?

标签: javanode.jsexpresshttp-post

解决方案


推荐阅读