How to submit a form html with Dart? - html

I need to scrape some data for the Dart / Flutter application and I need to log in to access the page.
How do I submit changes to the form data and click on the login button? I tried as follows:
var loginPage = await http.get('https://mypage.com/login');
var document = parse(loginPage.body);
var username = document.querySelector('#username') as InputElement;
var password = document.querySelector('#password') as InputElement;
username.value = 'USERNAME';
password.value = 'PASSWORD';
var submit = document.querySelector('.btn-submit') as ButtonElement;
submit.click();
But I have the following error:
Error: 'InputElement' isn't a type.
Error: 'ButtonElement' isn't a type.
I also tried the following:
InputElement username = document.querySelector('#username');
But a get the error A value of type 'Element' can't be assigned to a variable of type 'InputElement'
I need to make this scrape in the flutter application to avoid passing the password using API.
How can I log in to the page to get the data?

InputElement and ButtonElement are part of the dart:html package, therefore, we shouldn't forget to import it in our code:
import 'dart:html';
import 'package:http/http.dart' as http;
void submitForm() async {
var loginPage = await http.get('https://mypage.com/login');
var document = parse(loginPage.body);
var username = document.querySelector('#username') as InputElement;
var password = document.querySelector('#password') as InputElement;
username.value = 'USERNAME';
password.value = 'PASSWORD';
var submit = document.querySelector('.btn-submit') as ButtonElement;
submit.click();
}
By doing so, the compiler will recognize InputElement and ButtonElement as correct types.
Another, more elegant, way to obtain the same result would be:
import 'dart:html';
import 'package:http/http.dart' as http;
void submitForm() async {
var loginPage = await http.get('https://mypage.com/login');
Document document = parse(loginPage.body);
(document.querySelector('#username') as InputElement).value = 'USERNAME';
(document.querySelector('#password') as InputElement).value = 'PASSWORD';
(document.querySelector('.btn-submit') as ButtonElement).click();
}
If we'd like to use the same functionality in a Flutter project, we might need the help of the universal_html package:
Add this to your package's pubspec.yaml file:
dependencies:
universal_html: ^1.2.3
Run flutter pub get
Import the package and use the code:
import 'package:http/http.dart' as http;
import 'package:universal_html/html.dart';
void submitForm() async {
var loginPage = await http.get('https://mypage.com/login');
Document document = parse(loginPage.body);
(document.querySelector('#username') as InputElement).value = 'USERNAME';
(document.querySelector('#password') as InputElement).value = 'PASSWORD';
(document.querySelector('.btn-submit') as ButtonElement).click();
}

Related

Wp api for flutter

I am trying to use the wordpress api, but it throws me an error when entering the url.
Error: The argument type "String" can't be assigned to the parameter type Uri
Could someone explain the error to me and tell me how the code should look? Thanks
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<List> blog() async {
final response = await http.get(('https://bauk.blog/wp-json/wp/v2/posts?_embed': {"Accept": "application/json"}));
var convertirajson = jsonDecode(response.body);
return convertirajson;
}
You need to parse the url before passing it to http.get()
For that declare your url variable like this:
var url = Uri.parse('https://bauk.blog/wp-json/wp/v2/posts?_embed');
And then pass it to http.get() like this
http.get((url: {...} ));

How to use FIle module from Node.js without importing like new FIle([Blob],filename) in React

I'm working in MySQL Database class. In React Project, I can instantiate a File([Blob],filename). But in the Express Backend project, I can't use File module. It show an error as ReferenceError: File is not defined. I don't know how to use them without importing like the React project.
Here is my code for instantiating the File() Object. I tried to import it from "buffer"
downloadProfile = (id,success) => {
this.db.query(`SELECT pi.Filename, pi.Data AS Buffer, pi.MIME, pd.Data FROM profile_image AS pi JOIN profile_data AS pd ON pi.UserID = pd.UserID WHERE pi.UserID = ? AND pd.UserID = ?`,
[id,id],(err,result)=>{
if (err) throw err
const filename = result[0]["Filename"]
const buffer = result[0]["Buffer"]
const mime = result[0]["MIME"]
const image = this.getImageBase64(buffer,mime)
const imageBlob = new Blob([buffer],{type:mime})
const iamgeFile = new File([imageBlob],filename,{type:mime})
const data = JSON.parse(result[0]["Data"])
success({["Data"]:data,["Image"]:image})
})
}
In addition, which one between CommonJS and Module that recommend to working with Node.js Express project.

Web scraping giving different responses

I have this dart code, but it gives different outputs everytime i run it
Steps to reproduce
Add both files to same directory
Run
dart pub get
dart code.dart
It will create ten files having different contents
Create a file named code.dart
import "dart:io";
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart';
Future<String> scrapLyrics(String lyricsUrl) async {
final response = await http.get(Uri.parse(lyricsUrl));
var document = parse(response.body);
return document.getElementsByClassName('lyrics')[0].text;
}
main() async {
String ly;
for (int i = 1; i <=10; i++) {
print("Request number $i");
try{
ly= await scrapLyrics("https://www.genius.com/The-chainsmokers-closer-lyrics");
}
catch(e){
ly=e.toString();
}
File file = File("file$i.txt");
await file.writeAsString("$ly");
ly="";
}
}
Create a file pubspec.yaml
name: my_app
environment:
sdk: '>=2.10.0 <3.0.0'
dependencies:
http: ^0.13.3
html: ^0.15.0
Obtained Output(in some cases):
RangeError (index): Invalid value: Valid value range is empty: 0
The class name you're looking for is "Lyrics__Container-sc-1ynbvzw-8" not "lyrics"

Set property of ES6/ES2015 imported package

I am new in ES6 in ES5 I was able to set property of any require(imported) package something like this
var client = require('./client');
var conn = require('./conn/conn1.js');
client.conn = conn;
module.exports = client;
and in client package we were able to access client.conn. LIke this
function client(opts){
// client.conn is accesable here
}
Now in ES6 I am trying to do like this
import client from './client'
import conn from './conn/conn1.js'
client.conn = conn;
export {client as default}
But I am not able to access conn variable. How can I do it in the right way.
You can import the function direclty as import {client} from './client' this way the only thing that you will be importing of the client.js will be the client fucntion.
see: import statements

How to use HttpClient libraries to download a file from Google Drive?

I am attempting to integrate Google Picker on a page. The user uses the picker to sign in into his google account and picks a file. I am trying to use Apache HttpClient libraries to download this file.
Here are the implementation details :
I used Client Side Auth on Javascript as highlighted on this Google Tutorial. Once I got the 'fileId' of the file, I performed an authenticated GET using Google Drive API to retrieve the file metadata - name, user, dates, downloadUrl, exportLinks etc. You can use the above link to test the end point and see the responses if you have a fileId.
function postDownload (resp) {
var link;
var data = $.parseJSON (resp);
console.log (data.webContentLink);
if (data.mimeType == "application/pdf"){
link = data.downloadUrl;
} else {
link = (data.exportLinks)['application/pdf'];
}
//I wish to use the below link to download the file on server side
//using HttpClient libraries.
console.log ("Download Link - " +link);
}
//Call back after a file is selected on the picker
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var id = data.docs[0].id;
var accessToken = gapi.auth.getToken().access_token;
console.log (id);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.onload = function() {
postDownload(xhr.responseText);
};
xhr.onerror = function() {
checkAuth();
};
xhr.send();
}
}
Download Code:
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class DownloadFile {
public static void main(String[] args) throws IOException {
String link = "https://docs.google.com/uc? id=0ByJwoNOQMrO_S3hlWGRfa2JwVWM&export=download";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(link);
HttpResponse response = httpclient.execute(get);
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue());
}
}
}
If I use the link on the browser or the POSTMAN Chrome Extension, I am able to download the file correctly. The REST clients reports the incoming data correctly as 'application/pdf'.
However, when I use the above Snippet to perform a HttpGet on the URL, the incoming data has a content type of 'text/html' . No clue about this behavior. I don't think the final URL needs to be treated any differently to download the file.
Has anybody faced this issue? Thanks in advance.
I was able to solve the issue by adding an authentication header. I guess the auth token is stored in the user's session on the browser and hence I was able to download the file by hitting the URL. For a standalone Java program, we may have to pass the authentication token too.
The below code worked.
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class DownloadFile {
public static void main(String[] args) throws IOException {
String link = "https://docs.google.com/feeds/download/documents/export/Export?id=1vE4GVvVVipwMjKsc1q9p75LYWrRy8DZz0APjVFiHL9A";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(link);
// get.addHeader("Content-Type", "application/pdf");
get.addHeader("Authorization", "Bearer 1vE4GVvVVipwMjKsc1q9p75LYWrRy8DZz0APjVFiHL9A");
HttpResponse response = httpclient.execute(get);
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue());
}
}
}