Python Requests post error response in chat system - python

I want to chat and send a photo on a system by python automatically. but give me the following error. Can you show me how to fix it?
the way I do is by Python Requests post () Method
imformation for files Request URL
Request URL: https://11.chat.com/file-api/v2/teams/123/rooms/123/attachment/files
Request Method: POST
Status Code: 200
Remote Address: 11.111.11.11:11
Referrer Policy: strict-origin-when-cross-origin
access-control-allow-origin: *
access-control-expose-headers: Accept-Ranges,Content-Encoding,Content-Length,Content-Range
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Mon, 11 Apr 2020 1:36:05 GMT
chat-correlationid: 123-a0c0-11eb-ba75-bbcbc762f736
server: nginx/1.19.0 (Ubuntu)
vary: Accept-Encoding
x-powered-by: Express
:authority: 11.chat.com
:method: POST
:path: /file-api/v2/teams/123/rooms/123/attachment/files
:scheme: https
accept: application/vnd.tosslab.chat-v2+json
accept-encoding: gzip, deflate, br
accept-language: ja,en-US;q=0.9,en;q=0.8,es;q=0.7
authorization: bearer c12a8c2c975a23f73141320f6ad0fab108a6c03e1620fcb71772e5212b1aab745e32791d1b50bc2cb9ae6197e4f1d012
content-length: 103
content-type: application/json;charset=UTF-8
origin: https://www.chat.com
referer: https://www.chat.com/
sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36
x-account-id: 121c7942-b51d-4fba-932a-74f3df74b215
x-member-id: 1234
x-team-id: 1234
{fileName: "16188033640933e8796c2644e41653c1d0a6286c79c7c", title: "screenshot.jpeg"}
fileName: "16188033640933e8796c2644e41653c1d0a6286c79c7c"
title: "screenshot.jpeg"
imformation for POST Request URL
Request URL: https://11.chat.com/message-api/v1/teams/1234/rooms/1234/posts
Request Method: POST
Status Code: 200
Remote Address: 11.111.11.11:111
Referrer Policy: strict-origin-when-cross-origin
access-control-allow-origin: *
access-control-expose-headers: Accept-Ranges,Content-Encoding,Content-Length,Content-Range
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Mon, 11 Apr 2020 01:36:07 GMT
chat-correlationid: 600509c0-a0c0-11eb-8717-a38bfaeb5981
server: nginx/1.14.0 (Ubuntu)
vary: Accept-Encoding
x-powered-by: Express
:authority: 11.chat.com
:method: POST
:path: /message-api/v1/teams/1234/rooms/1234/posts
:scheme: https
accept: application/vnd.tosslab.chat-v2+json
accept-encoding: gzip, deflate, br
accept-language: ja,en-US;q=0.9,en;q=0.8,es;q=0.7
authorization: bearer c122a8c2c975a23f73141320f6ad0fab108a6c03e1620fcb71772e520b1aab745e32791d1b50bc2cb9a1237e4f1d07f6
content-length: 56
content-type: application/json;charset=UTF-8
origin: https://www.chat.com
referer: https://www.chat.com/
sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36
x-account-id: 121c7942-b51d-4fba-932a-74f3df74b215
x-member-id: 1234
x-team-id: 1234
{title: "TEST", content: "test", fileIds: [1234]}
content: "test"
fileIds: [1234]
title: "TEST"
imformation for files Request Response
{"id":111,"fromEntity":1234,"teamId":1234,"info":{"inviteUsers":[],"createInfo":{"members":[],"pg_members":[],"ch_members":[]}},"pollId":null,"feedbackType":null,"feedbackId":-1,"status":"created","messageId":1483745833,"time":1618803367054,"toEntity":[17274139],"message":{"id":1483745833,"teamId":13481330,"writerId":13489639,"contentType":"post","permission":740,"updatedAt":"2021-04-19T03:36:07.049Z","createdAt":"2021-04-19T03:36:07.049Z","isThreaded":false,"isEdited":false,"sharedMessages":[],"attachments":[{"content":{"connectInfo":[],"extraInfo":{"thumbnailUrl":"https://chat-box.com/files-thumb/13481330/16188033640933e8796c2644e41653c1d0a6286c79c7c","smallThumbnailUrl":"https://chat-box.com/files-thumb/13481330/16188033640933e8796c2644e41653c1d0a6286c79c7c?size=80","mediumThumbnailUrl":"https://chat-box.com/files-thumb/13481330/16188033640933e8796c2644e41653c1d0a6286c79c7c?size=320","largeThumbnailUrl":"https://chat-box.com/files-thumb/13481330/16188033640933e8796c2644e41653c1d0a6286c79c7c?size=640","width":985,"height":589},"todo":{},"drive":{},"filterType":"image","externalShared":false,"externalUrl":null,"externalCode":null,"fileUrl":"https://files.chat.com/files-private/1234/16188033640933e8796c2644e41653c1d0a6286c79c7c","serverUrl":"s3","ext":"jpeg","size":235967,"icon":"image","type":"image/jpeg","filename":"16188033640933e8796c2644e41653c1d0a6286c79c7c","name":"screenshot.jpeg","title":"screenshot.jpeg"},"contentType":"file","id":1111}],"mentions":[],"info":{"mention":[]},"likedCount":0,"commentCount":0,"formatKey":null,"isFormatted":false,"todoId":null,"postId":null,"pollId":null,"feedbackId":-1,"shareEntities":[1234],"status":"created","linkPreviewId":null,"content":{"title":"TEST","body":"test","connectInfo":[]},"updateTime":1618803367049,"createTime":1618803367049,"deleterId":null,"from":"web"}}
code
import urllib.request, json
import requests
import mimetypes
from requests_toolbelt import MultipartEncoder
from datetime import datetime as dt
#Header information
LOGIN_API_ENDPOINT = "https://11.chat.com/inner-api/token"
BOARD_POST_API_ENDPOINT = "https://11.chat.com/message-api/11/teams/{0}/rooms/{1}/posts"
BOARD_POST_FILE_UPLOAD_API_ENDPOINT = "https://upload.chat.com/file-api/11/teams/{0}/rooms/{1}/attachment/files"
BOARD_POST_COMMENT_ENDPOINT = "https://11.chat.com/-api/11/teams/{0}/posts/{1}/comments"
BOARD_GET_SEARCH_ENDPOINT = "https://11.chat.com/api/teams/{0}/search"
ROOM_ID = "12345"
#login information
login = {"username":"123",
"password":"123",
"platform":"web",
"grant_type":"password"}
url = LOGIN_API_ENDPOINT
method = "POST"
headers = {"Content-Type" : "application/json;charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
"Origin": "https://www.chat.com",
"Accept": "application/vnd.tosslab.chat-v4+json"}
# DateTime
strDate = dt.now().strftime("%Y/%m/%d")
strTime = dt.now().strftime("%H:%M:%S")
#Python for Json change
login_data = login
token_obj = {}
json_data = json.dumps(login_data).encode("utf-8")
# login
# http for POST
request = urllib.request.Request(url, data=json_data, method=method, headers=headers)
with urllib.request.urlopen(request) as response:
token_obj = json.loads(response.read().decode("utf-8"))
print(token_obj)
print(token_obj["token_type"])
print(token_obj["access_token"])
# chat server file upload
url = BOARD_POST_FILE_UPLOAD_API_ENDPOINT.format(TEAM_ID,ROOM_ID)
# --------------------------------------------1-------------------
#MIMETYPE change file
fileName = 'C:\\Users\\test\\Desktop\\screenshot.jpeg'
XLSX_MIMETYPE = mimetypes.guess_type(fileName)[0]
print(XLSX_MIMETYPE)
fileDataBinary = open(fileName, 'rb')
files = {'file': ('screenshot.jpeg', fileDataBinary, XLSX_MIMETYPE)}
#Uploading Data
m = MultipartEncoder(
fields={'title': 'screenshot.jpeg',
'file': ('screenshot.jpeg', fileDataBinary, XLSX_MIMETYPE)}
)
auth = "{0} {1}".format(token_obj["token_type"], token_obj["access_token"])
headers = {'Content-Type' : m.content_type, 'Authorization' : auth}
response = requests.post(url,data=m,headers=headers)
print(response.status_code)
print(response.content)
return_file_data = json.loads(response.content)
auth = "{0} {1}".format(token_obj["token_type"], token_obj["access_token"])
#Post chat
url = BOARD_POST_API_ENDPOINT.format(TEAM_ID,ROOM_ID)
print(url)
# print(return_file_data["id"])
fileList = []
fileList.append(return_file_data["id"])
payload = {"title":"test",
"content":"hello python world",
"fileIds":fileList}
title: "TEST"
print(payload)
headers1 = {'Content-Type' : 'application/json;charset=UTF-8', 'Authorization' : auth}
print(headers1)
response = requests.post(url,data=json.dumps(payload).encode("utf-8"),headers=headers1)
error
{'access_token': '12396dae54be29417882f5c5d72822121aa5a0', 'expires_in': 43200, 'refresh_token': '123139ec591043f23046f3b4b512134b5db4f7a445212550b6de60', 'token_type': 'bearer', 'ts': 1618805067661}
bearer
12396dae54be29417882f5c5d72822121aa5a0
image/jpeg
503
b''
Traceback (most recent call last):
File "c:/Users/test/Documents/test/test_post.py", line 78, in <module>
return_file_data = json.loads(response.content)
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
PS C:\Users\test>
If anyone knows, please let me know.

As per the error logs it seems you are getting empty data in response.
Status 503 means there might be error in API, so the response is empty.
You can directly use resp.json() method instead of json.dump(resp.content)
return_file_data = response.json()
Also add check for response.status_code == 200 and then take content from the response.

Related

Downloading files to client with django and GCP

I'm trying to download a file from Django to client using GCP. Currently, the request is made from axios, the file is fetched from GCP using url from the model. This file is then downloaded and returned as a HTTP response to the client. Inside the network tab, I can see a 200 OK response and the image is visible in the preview. However, the download is not being saved to the client desktop. I would appreciate any suggestions
Download method:
def download(request, pk):
url = Model.objects.get(id=pk).__dict__["file1"]
storage_client = storage.Client()
bucket = storage_client.get_bucket(setting("GS_MEDIA_BUCKET_NAME"))
blob = storage.Blob(url, bucket)
filename = url.rsplit("/", 1)[1]
file_path = "temp/" + filename
file_to_download = open(file_path, "rb")
mime_type, _ = mimetypes.guess_type(file_path)
response = HttpResponse(file_to_download, content_type=mime_type)
response["Content-Disposition"] = "attachment; filename=%s" % filename
return response
Response headers
HTTP/1.1 200 OK
Date: Fri, 29 Oct 2021 08:01:15 GMT
Server: WSGIServer/0.2 CPython/3.9.7
Content-Type: image/jpeg
Content-Disposition: attachment; filename=default.jpg
Vary: Origin
Access-Control-Allow-Origin: http://localhost:3000
X-Frame-Options: SAMEORIGIN
Content-Length: 10994
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Request headers
GET /api/models/download/26/ HTTP/1.1
Connection: keep-alive
sec-ch-ua: "Chromium";v="94", "Google Chrome";v="94", ";Not A
Brand";v="99"
Accept: application/json, text/plain, */*
sec-ch-ua-mobile: ?1
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5
Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/94.0.4606.81 Mobile Safari/537.36
sec-ch-ua-platform: "Android"
Origin: http://localhost:3000
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
It's a sad day for axios and content-disposition https://medium.com/#drevets/you-cant-prompt-a-file-download-with-the-content-disposition-header-using-axios-xhr-sorry-56577aa706d6
The solution will be along the lines of the following code, from the front-end:
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
link.click();
});

python request and get cookie

i have this request :
Host: subdomain.test.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Content-Length: 66
Connection: close
Referer: https://subdomain.test.com/login
Cookie: _gcl_au=1.1.1234502517.1571027917; _ga=GA1.2.763005402.1571027922; _fbp=fb.1.1571027923235.548273364; _hjid=9ae69402-9cd0-4e93-8e11-e6fdd78de9c3; PHPSESSID=12345c63db530475e3480d3c4e12345; _gid=GA1.2.1079574959.1571624463; _gat_UA-4593965-2=1
email=this#email.com&shownPassword=test123QWE&password=test123QWE
and this is my code :
import requests
from requests import session
payload = {
'email' : 'this#email.com',
'shownPassword' : 'test123QWE',
'password' : 'test123QWE'
}
with session() as c:
c.post('https://subdomain.test.com/login', data=payload)
response = c.get('https://subdomain.test.com/login')
print(response.cookies)
and this is the response :
<RequestsCookieJar[]>
so my question is, how can I get the cookie?
Thanks

Send HTTP Post with Python

I want to make a program where I can send HTTP post requests and respond.
So, I want to send THIS post:
POST https: //example.com/index.php?s=&&app=box&module=ajax&section=coreAjax&secure_key=&type=submit&lastid=87311&global=1 HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 10
Accept: text/javascript, text/html, application/xml, text/xml, */*
X-Prototype-Version: 1.7.2
Origin: https://example.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-type: application/x-www-form-urlencoded; charset=UTF-8
Referer: https://x.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: cookieconsent_status=dismiss;
And then enter the request body:
message= # Which I will make: "message= %s" % (messagex))
But I do not know how to send them and canĀ“t seem to find any way online, could someone help please?
The main parts are:
import requests # you have to install this library, with pip for example
# define your custom headers (as many as you want)
headers = {
'X-Prototype-Version': '1.7.2'
}
# define your URL params (!= of the body of the POST request)
params = {
'your_first_param': 'its_value',
'your_second_param': 'its_value'
}
# define the body of the POST request
data = {
'message' : 'your message'
}
# send the POST request
response = requests.post('https://example.com/index.php', params=params, data=data, headers=headers)
# here is the response
print response.text
Hope that helps.

Log in to website using requests

I am currently trying to get data off http://www.spotrac.com/ that requires being signed in. My current attempt uses this code(which I got by going through a bunch of other stack overflow questions on a similar topic)
from bs4 import BeautifulSoup as bs
from requests import session
payload = {
'id': 'contactForm',
'cmd': 'http://www.spotrac.com/signin/submit/',
'email': '*****',
'password': '*****'
}
with session() as c:
r_login = c.post('http://www.spotrac.com/signin/', data=payload)
print(r_login.headers)
response = c.get('http://www.spotrac.com/nba/cleveland-cavaliers/lebron-james')
print(response.cookies)
soup=bs(response.text, 'html.parser')
with open('ex.html','w') as f:
f.write(soup.prettify())
My current code does everything right, except I am not logged in when I'm making the request.
Thanks
You're sending POST request to a wrong URL, and with an incorrect payload as well.
POST http://www.spotrac.com/signin/submit/ HTTP/1.1
Host: www.spotrac.com
Connection: keep-alive
Content-Length: 86
Cache-Control: max-age=0
Origin: http://www.spotrac.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.spotrac.com/signin/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: cisession=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%2206021e191bdbbaf955f111f67b961056%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A11%3A%22119.9.105.6%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A108%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F55.0.2883.87+Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1485487245%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7Dd6089620b21ecce6837161605055ae04; _ga=GA1.2.910256341.1481865346; _gali=contactForm
redirect=http%3A%2F%2Fwww.spotrac.com%2F&email=sdfs%40gmail.com&password=lkasjdflksjad
HTTP/1.1 302 Found
Server: nginx
Date: Fri, 27 Jan 2017 04:21:16 GMT
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Set-Cookie: cisession=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22badb1275aee1cdad6736a6b4bb1ce809%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A11%3A%22119.9.105.6%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A108%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F55.0.2883.87+Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1485490876%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7Dad486866c32cac526487707cea85b8a9; expires=Fri, 10-Feb-2017 04:21:16 GMT; path=/
Location: http://www.spotrac.com/register/
X-Powered-By: PleskLin
MS-Author-Via: DAV
As you can see from above session, the correct url should be http://www.spotrac.com/signin/submit/, and payload string is redirect=http%3A%2F%2Fwww.spotrac.com%2F&email=sdfs%40gmail.com&password=lkasjdflksjad, which is basically:
payload = {'redirect': 'http://www.spotrac.com/',
'email': mail_address,
'password': password}
Also make sure simulate headers with correct parameters, then you're good to go.

Using Python Requests for ASP.NET authentication

I think I'm on the right track for ASP.NET authentication. I'm trying to use requests to pass credentials to a website. Here are the headers and network info I pulled from chrome:
Remote Address: REMOVED
Request URL: https://REMOVED/default.aspx
Request Method: POST
Status Code: 302 Found
Request Headers:
POST /default.aspx HTTP/1.1
Host: REMOVED
Connection: keep-alive
Content-Length: 928
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: https://REMOVED
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://REMOVED/default.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=REMOVED; BIGipServerpool_REMOVED_dmz_80=REMOVED.REMOVED.0000; AUTHCDB=**REMOVED**
Form Data:
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE: /wEP**REMAINDER REMOVED**
__EVENTVALIDATION: /wEd**REMAINDER REMOVED**
jsCheck:
ddlEngine:REMOVED:13008
Username:
Password:
btnLogin.x: 42
btnLogin.y: 9
btnLogin: Login
Response Headers:
Cache-Control: private
Content-Length: 132
Content-Type: text/html; charset=utf-8
Date: Fri, 13 Jun 2014 00:59:13 GMT
Location: /Dashboard.aspx
Server: Microsoft-IIS/7.5
Set-Cookie: AUTHCDB=**REMOVED**; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Here is the script I wrote:
import requests
FORM_DATA = {
"__EVENTTARGET:":,
"__EVENTARGUMENT:",
"__VIEWSTATE:/wEPDwUKMTA5NTA5ODU1MQ9kFgJmD2QWAgIGDxBkDxYFZgIBAgICAwIEF***REMAINDER REMOVED***",
"__EVENTVALIDATION:/wEdAAp4d3BHvSTs+Kv6cxGP3xEbBr8xrgRYad2tj4YCyRIw5qUAjimf****REMAINDER REMOVED****",
"jsCheck:",
"ddlEngine: REMOVED:13008",
"Username: ****",
"Password: ****",
"btnLogin.x: 42",
"btnLogin.y: 9",
"btnLogin: Login",
}
HEADER = {
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip,deflate,sdch",
"Accept-Language":"en-US,en;q=0.8",
"Cache-Control":"max-age=0",
"Connection":"keep-alive",
"Content-Type":"application/x-www-form-urlencoded",
"Host":"REMOVED",
"Origin":"REMOVED",
"Referer":"REMOVED",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
}
LOGIN_URL = "REMOVED"
#requests session to handle cookies.
s = requests.Session()
#Send a POST request with the form data/header info
r = s.post(LOGIN_URL, data=FORM_DATA, headers=HEADER)
if r.status_code == 302:
print "Successfully logged in."
else:
print "Error logging in."
Am I able to use Python Requests to log into a webpage that uses ASP.NET? If so, is this the correct way to pass the credentials into the website? For reference, the website I'm trying to log into is a company server monitor.
Looks like a similar issue was posted here and here. I've been using RoboBrowser and it's made messing with ASPX so much simpler.
from robobrowser import RoboBrowser
login_url = 'http://example.com/Login.aspx'
username = 'JohnDoe'
password = 'passwd'
browser = RoboBrowser(history=True)
# This gets all the ASPX stuff, __VIEWSTATE and friends
browser.open(login_url)
signin = brower.get_form(id='aspnetForm')
signin["jsCheck"].value = ''
signin["ddlEngine"].value = "REMOVED:13008"
signin["Username"].value = username
signin["Password"].value = password
signin["btnLogin.x"].value = "42"
signin["btnLogin.y"].value = "9"
signin["btnLogin"].value = "Login"
browser.submit_form(signin)
I'm also working with some ASP.net pages right now, and being somewhat familiar with the requests module, I thought I'd try to help out a bit.
It is my understanding that requests supports basic authentication in this fashion:
from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
It could be the case that you'll need to import a different authentication library that works with ASP.net and plug that directly into the auth function of requests.
Hope this helps!

Categories

Resources