简体   繁体   中英

How to get single value from request response

I started writing a program that will automate user actions, by now it's meant to be an easier menu to make faster actions by just sending requests to the official website by clicks on my own page. (something like web-bot but not exactly). My problem is when i send login request in response i get back user_id, server, session_id etc. And I need to save that session_id to make the other actions. How can i save this to variable. All in JavaScript.

I was looking in the internet since yesterday for an answer and i still can't find (or understand) how to get this I tried function login(){ fetch('url', { method: 'POST', headers: { //headers }, body: //my Id's }) })

//There's the problem to solve .then(res => res.text()) .then(data => obj = data) .then(() => console.log(obj)) console.log(body.session_id);

// I even tried the substring but 1. It won't work as I want because There are sometimes //more and less letters. 2. I get and error "Cannot read properties of undefined (reading //'substr')"

`session = obj;
session_id = session.substring(291,30)
console.log(session_id)`

It looks like you're using the text() method on the Response object returned from fetch() , which will give you a string representation of the response.

You probably want to be using the json() method instead, and from that you can get your session_id .

This guide has some more useful information that may help you: https://javascript.info/fetch

Ok it works now with

`async function login(){ let session = await fetch('url', {
    //code
}
let result = await session.json();
console.log(result);
session_id = result.data.user.session_id;
user_id = result.data.user.id;`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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