-1

Thx for answer in advance, here's the code:

const request = require('request');
const cheerio = require('cheerio');
const url = 'https://gamehag.com/api/v1/register';
let csrfToken;

const form = {
    name: 'nic0l4s171',
    email: 'nic0l4s171@gmail.com',
    password: 'nic0l4s171'
};

request('https://gamehag.com/', (err, res, body) => {
    if(err) return console.error(err);

    let $ = cheerio.load(body);

    csrfToken = $('meta[name=csrf-token]').attr('content');

    const headers = {
        'User-Agent': 'user-agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'x-csrf-token' : csrfToken
    };

    request.post({url, form, headers}, (err, res, body) => {
        console.log(body);
    });
});

This is the error i get when trying to execute the code: "error code: 1010" Image

dave
  • 1
  • 1
  • Please specify your problem in more detail. What did you try? Which line of code causes the error? – mozkomor05 Jul 05 '20 at 12:01
  • The error does not come from nodeJS, but from the server. The line of code is this one "console.log(body);", as i said the server return me this (err) instead of returning something when all went good ^^ – dave Jul 05 '20 at 12:04
  • It looks like you are getting back HTTP 403 error (forbidden), but not sure why. I couldn't find API documentation on `gamehag.com` page, do you have a link for it? – errata Jul 05 '20 at 12:06
  • I don't know how gamehag.com request should look like. However I think the body doesn't contain meta properties. Try to print `csrfToken` to console. If it is empty then you need to read headers from `res`. – mozkomor05 Jul 05 '20 at 12:08
  • The server checks that you have a valid User-Agent. If you include a valid one, the error disappears (`user-agent: Mozilla...` is not valid, it should just be `Mozilla...`) – blex Jul 05 '20 at 12:27
  • Actually, i don't think there's an public api for gameHag, does it ? I checked the User-Agent header thx. – dave Jul 05 '20 at 12:45

1 Answers1

0

I think, this code block can help you.

        var request = require('request');
        var options = {
            'method': 'POST',
            'url': 'https://gamehag.com/api/v1/register',
            'headers': {
               'x-csrf-token': 'roglJups3F9dF3FpOntpwDYqq7UFJQU4BOAaYX3z',
               'Content-Type': 'application/x-www-form-urlencoded'
        },
        form: {
            'name': 'nic0l4s171',
            'email': 'nic0l4s171@gmail.com',
            'password': 'nic0l4s171'
        }
     };
    request(options, function (error, response) {
        if (error) throw new Error(error);
        console.log(response.body);
    });
  • Seems to be working in the console. However, when it comes to try to connect via the website in question it says that there'no such user... why ? – dave Jul 05 '20 at 12:47