Scraping an ASP.NET website with NodeJS - asp.net

i am trying to login to my supplier website programatically and get the resseller price by code. i have my username and password provided by the supplier to use in the website which is powred by ASP.NET. i tried to use the request module but got no luck with it.
this the code i used so far :
var request = require('request');
var j = request.jar();
var request = request.defaults({ jar : j }) //it will make the session default for every request
//...
request({
url:"https://www.XXXXX.com/login.aspx",
method:"POST",
form:{
ctl00$cpholder$txtUserName:"YYYYYYYY",
ctl00$cpholder$txtPassword:"ZZZZZZZZ"
}
},
function(err,response,body){
console.log(err);
// here i try to access the product page like a reseller
request({
url:"https://www.XXXXXX.com/productdetails.aspx?id=20000028&itemno=90NB0CL1-M08420",
method:"GET",
}, function(err, response, body){
console.log(err);
//Some logic
});
});
}
this is the login form code ( in pastebin because it is very long )
https://pastebin.com/kwuRdLX4
please help

Related

In Meteor with Iron router how would I redirect to a thank you URL stored in the database after form submission

I have a system written in meteor where when someone submits a form it currently just shows a thankyou message in a modal. My client wants to be able to allow for multiple different thank you pages based on a thank you URL field on a clients record.
I am using iron router. I have successfully setup a server side route but I'm unable to even call that.
What I would like is to on form submission update the database then pull the clients thankyou URL and redirect the user to that. Sorry for not code to tweak but I'm just looking for the best way to handle it.
UPDATE:
Client Side
Meteor.call(
"addSendLeads",
{
bookingType: bookingType,
ad_platfrom: ad_platform,
senderID: email,
facebookPageID: fbPageID,
first_name: first_name,
last_name: last_name,
event_name: event_name,
startDate: startDate,
email: email,
phone: phone,
comments: comments,
},
(err) => {
if (err) {
console.log(err);
swal(
"Error",
"Sorry, there was an error. Please try again later."
);
} else {
target.firstName.value = "";
target.lastName.value = "";
target.email.value = "";
target.phone.value = "";
target.comments.value = "";
Router.go("/thankyou/" + url);
}
}
);
The Route in server/routes.js
Router.route("/thankyou/:url", { where: "server" }).get(function () {
this.response.writeHead(302, {
Location: url,
});
this.response.end();
});
Hope this helps to clarify...
Your submit form method should return your user custom URL, unless you pull that in the initial stage when you open the form.
With a method, you have a callback (pre Node16) or a .then(result => do_something_with_it). Async methods are already usable in Meteor 2.10.
Your call back or your async effect can call your routing:
Meteor.callAsync('submitForm', form) // or Meteor.call('name', {object data}, (err, res) => do_something_with_res)
.then(url => Router.go(url)
.catch(methodErrors => console.log(methodErros))

Posting Comments to WordPress with WPAPI

I'm using Netlify event-triggered webhooks to call a script that's designed to post a new comment to the WordPress API. I'm trying to implement wpapi to make the POST request but not sure if I'm hooked up properly.
exports.handler = async (event, context, callback) => {
let body = JSON.parse(event.body).payload
if (body.form_name == 'comment-form') {
// I assume I have to authenticate here
var wp = new WPAPI({
endpoint: 'https://example.com/wp-json',
username: 'username',
password: '123456'
});
...
I then form the data to pass in... From what I can tell from the WordPress REST API, I can pass in a name, comment, and a post id. I'm not sure if I'm missing a parameter as I can't find any documentation about required params.
// url encode - not sure if this is required
let comment = {
author_name: encodeURI(author_name),
author_comment: encodeURI(author_name),
post: body.data.postId
}
I then try calling wp.comments().create() passing in the object and setting up a callback:
wp.comments().create(comment, function(args) {
console.log(args) }
).then(function( response ) {
console.log( response );
}).catch(function (err) {
console.log(err);
});
I am using this function in a Gatsby project and am utitlizing gatsby-source-wordpress to pull data from a WordPress site, if that makes any difference.
When I run this function in Netlify, in the function log, there is no response or error message.
Thanks

How to insert a username into a post in meteor

I want to insert the username along with the post. When I tried adding a post using Post.insert({content: content}) the post got inserted and displayed on the client successfully. Now I try to insert a username also, so I try like this:
var username = new Meteor.user().username;
Post.insert({content: content, username: username});
Now not only the username not inserted it fails to insert the content as well. No error. What am I doing wrong?
The above is the problem. But I did a mistake and deleted insecure package so now I can not check the results from console or inserting directly at client. So the following is the method I used. (Please be patient, I'm new to meteor, if I ask stupid questions, pardon me).
In imports/both/post.js I put:
export const Post = new Mongo.Collection('post');
In server/main.js
import { Post } from '/imports/both/post.js';
Meteor.methods({
addPost: function (content) {
var user = Meteor.users.findOne(this.userId);
var username = user ? user.username : 'Anonymous';
Post.insert({content: content, username: username});
}
});
In client/template/postsForm.js, the following code:
import { Post } from '/imports/both/post.js';
Template.postsForm.events({
'submit form': function(event){
event.preventDefault();
var content = document.getElementById('content').value;
Meteor.call('addPost', content);
event.target.reset();
}
});
I can insert a post from server and get displayed on client but from client side I can't. And I get no error.
are you sure it's failing on the insert? you have a "new" in front of Meteor.user(), i suspect it's failing there. try the following, which includes a null check on the user and shows any error coming from the insert:
let username = Meteor.user() && Meteor.user().username;
Post.insert({content: content, username: username}, function(error) {
if (error) {
console.error(error);
}
};
you also have 'content' in quotes, which looks wrong.

How to save an element in a collection with data fetched from a webservice api

A user saves a trip (from a city to another one) and before storing it into the mongo collection, my app have to fetch the trip distance and time from the mapquest api.
How and where would you put the HTTP.call ? Server side ? Client side ?
Install http module:
meteor add http
Create a server method to call web service. Here is my example where the user put URL and the code returns title of page.
Server code:
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Meteor.methods({
getTitle: function(url) {
var response = Meteor.http.call("GET", url);
return response;
}
});
And here is a client code:
Template.new_bookmark.events({
// add new bookmark
'keyup #add-bookmark' : function(e,t) {
if(e.which === 13)
{
var url = String(e.target.value || "");
if(url) {
Meteor.call("getTitle", url, function(err, response) {
var url_title = response.content.match(/<title[^>]*>([^<]+)<\/title>/)[1];
var timestamp = new Date().getTime();
bookmarks.insert({Name:url_title,URL:url,tags:["empty"], Timestamp: timestamp});
});
}
}
}
});
If the user press "enter" in the #add-bookmark field, I get fields value and pass it to server method. The sever method returns page HTML source and I parse it on client, get title and store it on my collection.

How to correctly login user in mobile app built with PhoneGap

Im building a mobile app on a PhoneGap. I've created several static HTML pages (stored on the phone locally) and trying to load some data from server using AJAX.
$( document ).bind( "mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
$(document).ready(function () {
ajaxDeferred = $.ajax({
url: 'http://example.com/api/isAuthorized'
type: method,
data: parameters,
timeout: 40000
});
ajaxDeferred
.done(function(data) {
alert(data.result);
})
.fail(onFailHandler);
});
On a server side I have this code
public ActionResult IsAuthorized()
{
FormsAuthentication.SetAuthCookie("shade", true);
return this.Json(
new
{
result = this.HttpContext.User.Identity.IsAuthenticated
},
JsonRequestBehavior.AllowGet
);
}
I expect:
On the first run recieve "False" result since user is not authorized
On the second run recieve "True", because of SetAuthCookie
When I try it in a browser, it works as expected.
But whatever I run mobile app i alwayes recieve False, like cookies are not being tranfered.
So the question is how to correctly login user with AJAX, is it possible?
What are best practises? For unknown reason this simple scenario is not covered by PhoneGap manuals.
Ok, I found an answer:
$(document).ready(function () {
...
});
is not suitable. Instead one have to use
document.addEventListener(
"deviceready",
function() {
... at this point cookies are working fine!
},
false);
Access-Control-Allow-Origin and Access-Control-Allow-Credentials headers are not required as well as other cross-domain techniques

Resources