Coming soon: Your brand new Help Center & Community! Get a sneak-peek here

http.post login session and navigate on logged in site

Hi,

Im looking into move a python script we have that login and read data from one of our vendors webpage. I use Python request library for this today but would love to get it in crmscript.

I did some testing with following:

HTTP http;
http.setValue("loginname", "MyUser");
http.setValue("password", "MyPass");
Byte[] a = http.post("http://www.testsite.com/login.php?");

if (http.hasError())
print(http.getErrorMessage());
else
print(String(a));

The above works fine, I get navigated to the logged in page but what would be the next step to read the data from the site when logged in?

I did try:

Byte[] b = http.get("http://www.testsite.com/home/main.php?Number=222222222&action=search");

But that navigates me to loginpage as if there is a new session.

 

Suggestion on how I should proceed?

 

RE: http.post login session and navigate on logged in site

It really depends on how authentication works in your site. 

The http class has support for different kind of authentications, where basic auth is the default. This can be changed by using the http.setOption("authenticationMethod", <auth method>).

In addition, you can set the username and password using http.setOption("username", <username>) and http.setOption("password", <password>).

If your endpoint does not support any of these methods, or a way to use token or something, then you need check what your browser does when you log into the site. Could it be that it gets a session id back as a cookie?

 

Af: Stian Andre Olsen 28. sep 2021

RE: http.post login session and navigate on logged in site

Hi,

So my first code works and it logs in with http.setValue. But when I try to do http.get to read the data I want it seems to be redirecting me to login site again. I understand that Pythons requests library do handle sessions but do I need to add something so my http.get is in same sessions?

HTTP http;
http.setValue("loginname", "MyUser");
http.setValue("password", "MyPass");
Byte[] a = http.post("http://www.testsite.com/login.php?");

if (http.hasError())
print(http.getErrorMessage());
else
print(String(a));

http.setValue("Number","222222222");
http.setValue("action","search");
Byte[] b = http.get("http://www.testsite.com/home/main.php?Number=222222222&action=search");

print(String(b));

It seems to be a PHPSESSID cookie for the page

 

Af: Johan Friberg 28. sep 2021

RE: http.post login session and navigate on logged in site

Hi Johan,

in Python3, I would think that you are using Session from the requests module? If so, then the cookie set by the response from /login will be resent when you do the next request.

The HTTP class in CRMScript doesn't do that for you, unfortunately. You'll need to grab the cookie from the first response, and then pass it along in the second call.

Af: Frode Lillerud 29. sep 2021

RE: http.post login session and navigate on logged in site

Hi Frode,

Thanks for the input, you are right i use Session from request module and it works good. I would like to get it working directly instead of having this solution.

I tried grabbing the cookie like this:

 

HTTP http;
http.setValue("loginname", "MyUser");
http.setValue("password", "MyPass");
Byte[] a = http.post("http://www.testsite.com/login.php?");
String SCookie = http.getResponseHeader("Set-Cookie");
if (http.hasError())
print(http.getErrorMessage());
else
print(String(a));


http.addHeader("Cookie",SCookie);
Byte[] b = http.get("http://www.testsite.com/trace/main.php?snnsel=300492527&action=search");

print(String(b));

It still will not give me the result page, using HttpDebug it looks to me that the cookie is the same

Af: Johan Friberg 4. okt 2021