【问题标题】:POST request returning text/html and not JSON, how do I get the JSON in JS?POST 请求返回文本/html 而不是 JSON,如何在 JS 中获取 JSON?
【发布时间】:2019-05-08 05:12:44
【问题描述】:

为了工作,我们通过州网站验证选民登记。最近的更新破坏了我们的验证。

他们现在需要一个 XSFR 令牌/cookie。我已经能够使用下面的代码检索 cookie 并在 POST 请求中提交它。服务器以代码 200 响应。如果您注释掉 cookie/XSFR,您将看到它以 403 响应。

我正在使用请求模块。服务器响应 HTML/文本文件,而不是像浏览器中那样的 JSON 文件。我究竟做错了什么?如果信息是错误信息,服务器仍会以 JSON 文件响应,我已经包含了一些虚拟信息。任何帮助都会得到帮助!

我已删除我们用于选民验证的链接,并根据已修复的问题将其替换为 Google.com。

//using request module    npm install request --save

var request = require('request');
var jar = request.jar();
var request = request.defaults({
jar: jar,
});
var jar = request.jar();

// get cookie for XSRF token

request.get({
    url: 'https://www.google.com',
    method: 'get',
    jar: jar
}, () => {
    cookies = jar.getCookies('https://www.google.com');
    //output cookie
    console.log(cookies);
    var cookieToString = cookies.toString()
    //slice token for cookie response
    var xsrfCookie = cookieToString.slice(0, 47)
    //slice token for token response
    var slicedCookie = cookieToString.slice(11, 47)

    //send POST with xsrf token & cookie

    var request = require('request');
    var options = {

        uri: 'https://www.google.com',
        headers: {
            //custom HTTP headers for response
            Host: 'votesearch.google.com',
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0',
            Accept: 'application/json, text/plain, */*',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate, br',
            Referer: 'https://www.google.com',
            'Content-Type': 'application/json;charset=utf-8',
            'X-XSRF-TOKEN': slicedCookie,
            //'Content-Length': '118', leave commented out or server response hangs - not sure why
            Connection: 'keep-alive',
            Cookie: xsrfCookie,
            Pragma: 'no-cache',
            'Cache-Control': 'no-cache'
        },
        method: 'POST',
        json: {
            city: 'sometown',
            dob: '01-01-1950',
            firstName: 'John',
            lastName: 'Doe',
            street: '1234 street',
            zip: '12345'
        }
    }
    //server responsds with content type 'text/html' not JSON like in browser
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body.id) // Print the shortened url - not working.
            console.log(response.headers); // can I get JSON from this?
            console.log(response.statusCode)


        } else {
            console.log('response code ' + response.statusCode);
            console.log('error ' + error)
        }
    });

})

【问题讨论】:

    标签: javascript json post http-post


    【解决方案1】:

    使用request 模块发出请求时,为了在请求中包含 JSON 编码的请求正文,应将 body 选项设置为 JSON 可序列化对象,并设置 json 字段到true

    json 字段设置为true 还将确保响应将被解析为JSON 并在响应的body 字段中可用。

    在你的代码 sn-p 中的第二个请求中,json 字段设置为一个对象,该对象不是有效值。

    【讨论】:

    • 你就是那个男人,我只是想让你知道我已经把它修好了并且可以正常工作。谢谢!
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多