Linux curl form login or submission and cookie use

Keywords: Linux curl encoding JSON

This article mainly explains the implementation of form submission login through curl.A separate form submission is similar to a form login, so it's not a separate matter.

Note: Login for curl form submission does not apply to all websites because some websites have limitations or other checks in the background.We don't know what the restrictions or validation mechanisms behind these websites are, so direct curl form login may not work.

Of course, the following is a case where curl can be used to log in.

 

Case: LeanCloud login

Requirements and results

Requirements: After logging in through curl, you can normally access the application page of leancloud.

The login page is linked as follows:

1 https://leancloud.cn/dashboard/login.html#/signin

 

The following pages can be accessed normally:

1 https://leancloud.cn/dashboard/applist.html#/apps

 

Browser access effect:

 

No login direct access results

Browser Access Results

 

The access connections in red box 403 above are as follows:

1 https://leancloud.cn/1.1/clients/self/apps

 

Verify login through curl

 1 [root@iZ28xbsfvc4Z ~]# curl -i https://leancloud.cn/1.1/clients/self/apps
 2 HTTP/1.1 403 Forbidden
 3 Server: openresty
 4 Date: Sun, 14 Jul 2019 11:35:28 GMT
 5 Content-Type: application/json;charset=utf-8
 6 Transfer-Encoding: chunked
 7 Connection: keep-alive
 8 Vary: Accept-Encoding
 9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11 
12 {"code":1,"error":"User doesn't sign in."}

 

Get form field information

 

Get form submission links

Link information for form submission can be obtained from the following figure.Specifically as follows:

1 https://leancloud.cn/1.1/signin

 

curl form login and save cookie information

1 curl -v -c leancloud1.info -X POST -F 'email=yourname' -F 'password=yourpassword' https://leancloud.cn/1.1/signin
2 # Or
3 curl -v -c leancloud3.info -X POST -d 'email=yourname&password=yourpassword' https://leancloud.cn/1.1/signin

 

View cookie information

 1 [root@iZ28xbsfvc4Z 20190714_02]# ll
 2 total 32
 3 -rw-r--r-- 1 root root  337 Jul 14 19:45 leancloud1.info
 4 -rw-r--r-- 1 root root  335 Jul 14 19:46 leancloud3.info
 5 [root@iZ28xbsfvc4Z 20190714_02]# cat leancloud1.info 
 6 # Netscape HTTP Cookie File
 7 # http://curl.haxx.se/docs/http-cookies.html
 8 # This file was generated by libcurl! Edit at your own risk.
 9 
10 #HttpOnly_leancloud.cn    FALSE    /    TRUE    1563709522    uluru_user    Ff1IPOiMX%2F6ipevuxy0OOg%3D%3D
11 leancloud.cn    FALSE    /    TRUE    1563709522    XSRF-TOKEN    5647dc84bd6eaea37eca2d07ae0e401cca4ba76803989c8559XXXXX7283da
12 [root@iZ28xbsfvc4Z 20190714_02]# cat leancloud3.info 
13 # Netscape HTTP Cookie File
14 # http://curl.haxx.se/docs/http-cookies.html
15 # This file was generated by libcurl! Edit at your own risk.
16 
17 #HttpOnly_leancloud.cn    FALSE    /    TRUE    1563709591    uluru_user    arTwQm6JylzLjBaQt7TpiQ%3D%3D
18 leancloud.cn    FALSE    /    TRUE    1563709591    XSRF-TOKEN    751e12827c7c046408541bc1bf962b5912ac35b0d07f88120XXXXXX40704704

Field description for each column:
Domain: The domain name of a variable that is created and readable.
flag: A TRUE/FALSE value indicating whether all machines in a given domain can access the variable.This value is set automatically by the browser, depending on the value you set for the domain.
Path: The path to which the variable is valid in the domain.
Secure: A TRUE/FALSE value indicating whether a secure connection to the domain is required to access the variable.
expiration: The UNIX time the variable will expire.UNIX time is defined as the number of seconds starting at 00:00:00 GMT on January 1, 1970.
Name: variable name
Value: variable value

 

Verify login success

Direct access and access with cookie s, two ways of access, please compare.

Direct Access

 1 [root@iZ28xbsfvc4Z 20190714_02]# curl -i https://leancloud.cn/1.1/clients/self/apps
 2 HTTP/1.1 403 Forbidden
 3 Server: openresty
 4 Date: Sun, 14 Jul 2019 11:52:47 GMT
 5 Content-Type: application/json;charset=utf-8
 6 Transfer-Encoding: chunked
 7 Connection: keep-alive
 8 Vary: Accept-Encoding
 9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11 
12 {"code":1,"error":"User doesn't sign in."}

 

Access with cookie file

 1 # Use cookie s
 2 [root@iZ28xbsfvc4Z 20190714_02]# curl -i -b leancloud1.info https://leancloud.cn/1.1/clients/self/apps 
 3 ## perhaps
 4 [root@iZ28xbsfvc4Z 20190714_02]# curl -i -b leancloud3.info https://leancloud.cn/1.1/clients/self/apps
 5 HTTP/1.1 200 OK
 6 Server: openresty
 7 Date: Sun, 14 Jul 2019 11:53:29 GMT
 8 Content-Type: application/json;charset=utf-8
 9 Transfer-Encoding: chunked
10 Connection: keep-alive
11 Vary: Accept-Encoding
12 Cache-Control: no-cache,no-store
13 Pragma: no-cache
14 Strict-Transport-Security: max-age=31536000
15 
16 [{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ..................

 

Copy cookie access from browser

 1 [root@iZ28xbsfvc4Z 20190720]# curl -i -H 'cookie: _ga=GA1.2.2055706705.1560005524; ............' https://leancloud.cn/1.1/clients/self/apps
 2 HTTP/1.1 200 OK
 3 Server: openresty
 4 Date: Sat, 20 Jul 2019 08:11:37 GMT
 5 Content-Type: application/json;charset=utf-8
 6 Transfer-Encoding: chunked
 7 Connection: keep-alive
 8 Vary: Accept-Encoding
 9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11 Strict-Transport-Security: max-age=31536000
12 
13 [{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ..................

curl is known to have logged in successfully.

 

Recommended reading

Linux curl command details

Common Linux curl examples

Linux curl form login or submission and cookie use

 

 

If you think it's good, click on a compliment (-^O^-)!

-—END-—-

Posted by JoCitizen on Sat, 10 Aug 2019 20:44:22 -0700