繁体   English   中英

执行 POST 请求时,无法在 XMLHttpRequest 的 node.js 版本上正确发送请求正文

[英]Can't properly send Request Body on the node.js version of XMLHttpRequest when doing POST request

我正在尝试使用 NodeJS 和此处找到的 XMLHttpRequest 库向 Microsoft Oauth2.0 令牌 URL 发出 PSOT 请求。 但是,我遇到了问题,即我无法将正确的请求正文与请求一起发送。 我已经尝试从这里使用FormData ,我尝试了URLSearchParams ,并且我尝试按照我们从 GET 请求中的地址栏中知道的方式将其设置为 String 。 下面您可以看到我尝试在 GET URL 表单中执行此操作时的代码,在我推荐的部分中,您可以看到我尝试使用 FormData 代替。

var xhr = new XMLHttpRequest();
        xhr.open("POST", 'https://login.microsoftonline.com/common/oauth2/v2.0/token');

        /*var data = new FormData();
        //var data = new URLSearchParams();
        data.append('client_id', clientId);
        data.append("grant_type", "authorization_code");
        data.append("scope", "openid email profile");
        data.append("code", code);
        data.append("redirect_uri", "http://" + req.headers.host + req.url);
        data.append("client_secret", secret);
        Error message on this one: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of FormData
Same message with URLSearchParams, except it received an instance of URLSearchParams
*/

        var data = 'client_id=' + clientId;
        data += '&grant_type=authorization_code';
        data += '&scope=openid%20email%20profile';
        data += '&code=' + code;
        data += '&redirect_uri=' + encodeURIComponent("http://" + req.headers.host + req.url);
        data += '&client_secret=' + secret;
        //This one gives me an error message from Microsoft: {"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: ratherNotSay\r\nCorrelation ID: ratherNotSay\r\nTimestamp: 2020-02-10 10:37:36Z","error_codes":[900144],"timestamp":"2020-02-10 10:37:36Z","trace_id":"ratherNotSay","correlation_id":"ratherNotSay","error_uri":"https://login.microsoftonline.com/error?code=900144"}
//This must mean that the request body can not have been submitted in the right way.

        xhr.onreadystatechange = () => {
          if (xhr.readyState == 4) {
            console.log(xhr.status + "\n" + xhr.responseText + "\n");
          }
        };
        xhr.send(data);

您可以使用toString()方法将URLSearchParams实例转换为查询字符串,就像您手动构建的那样。

我不知道在node-XMLHTTPRequest版本中Content-Type标头是否默认设置为application/x-www-form-urlencoded ,但手动设置它不会有什么坏处。

const xhr = new XMLHttpRequest();
xhr.open("POST", 'https://login.microsoftonline.com/common/oauth2/v2.0/token');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

const data = new URLSearchParams();
data.append('client_id', clientId);
data.append("grant_type", "authorization_code");
data.append("scope", "openid email profile");
data.append("code", code);
data.append("redirect_uri", "http://" + req.headers.host + req.url);
data.append("client_secret", secret);

xhr.onreadystatechange = () => {
    if (xhr.readyState == 4) {
        console.log(xhr.status + "\n" + xhr.responseText + "\n");
    }
};

xhr.send(data.toString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM