A
A
Alexander Koshelev2018-06-05 22:16:13
Node.js
Alexander Koshelev, 2018-06-05 22:16:13

Why does it give an error when working with the fetch module?

Good afternoon guys, I'm starting to work with the fetch module and, for example, I wanted to try to log in to a third-party site, for this I chose one of the sites.
I checked the headers during authorization and entered them into the headers object, I
run the code and, as a result, the error is 422 (the server successfully accepted the request, it can work with the specified type of data (for example, the request body contains an XML document that has the correct syntax), but there is some then a logical error, due to which it is impossible to perform an operation on the resource), what is wrong with the code ??? help me please!!!

const fetch = require('node-fetch');
var server = 'https://ask.fm/login';

myHeaders = ({
  Cookie: "Вставил Сюда Кукис",
  "Origin": "https://ask.fm",
  "Accept-Encoding": "gzip, deflate, br",
  "Accept-Language": "ru,en-US;q=0.8,en;q=0.6,zh;q=0.4,zh-TW;q=0.2,zh-CN;q=0.2",
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
  "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  "Accept": "application/json, text/javascript, */*; q=0.01",
  "Referer": "https://ask.fm/login",
  "X-Requested-With": "XMLHttpRequest",
  "Connection": "keep-alive",
  "Body": {utf8: "✓",
        authenticity_token: "NlYqtcoegnDU5gyBqgMX/AM0/YekJLZRrt/wohWEeN5ZLOXPwxQXUjUxFT5CwYpQEmWcZ7Sq4+zfkDYBGKORZw==",
        login: "[email protected]",
        password: "1q2w3e4r",
        remember_me: 1
  }
});

fetch(server, { method: 'POST', headers: myHeaders})
  .then((res) => {
    console.log(res)
  return res.json()
  })
  .then((json) => {
    console.log(json)
  })
  .catch(function(error) { console.log(error); });

Here is the console response
Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {

        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://ask.fm/login',
     status: 422,
     statusText: 'Unprocessable Entity',
     headers: Headers { [Symbol(map)]: [Object] } } }
{ error: 'Error' }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mihmig, 2018-06-26
@mihmig

I see that the request body is in the application/x-www-form-urlencoded format. You pass it in the request headers
And you need it in the body itself (use formData)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question