I'm trying to use Python post request to automate the refreshing of a summoner profile in OP.GG:
On the page there is a button that calls the following javascript file: https://opgg-static.akamaized.net/js3/summoner.js?1492755586
Navigating to this URL: https://na.op.gg/summoner/userName=hamski
Running the follow snippet of code in Chrome's console works perfectly. The profile refreshed as expected.
$.OP.GG.ajax.getJSON({
url: ('/summoner/ajax/renew.json/'),
method: 'post',
data: {
summonerId: 47220368
},
callback: {
onJSON: function(json){
console.log(json)
},
onError: function(error){
$.OP.GG.summoner.renewBtn.stop(btn);
alert(error);
}
}
});
I did a post request using the requests module
import json
import requests
url = "https://na.op.gg/summoner/ajax/renew.json/"
payload = {
'summonerId': 47220368,
}
data=json.dumps(payload)
print data
r = requests.post(url, data)
print r
print r.status_code
print r.text
The result was a 418 error.
<Response [418]> 418
r.text basically gives me the html of this page: https://na.op.gg/summoner/ajax/renew.json/
Updated code:
import json
import requests
from fake_useragent import UserAgent
ua = UserAgent(cache=False)
url = "https://na.op.gg/summoner/ajax/renew.json/"
payload = {
'summonerId': 47220368,
}
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent':ua.random
})
print ua.random
session = requests.Session()
r1 = session.get("https://na.op.gg/summoner/userName=hamski",headers=headers )
print session.cookies.get_dict()
r = requests.post(url, data=json.dumps(payload),cookies=r1.cookies, headers=headers)
print r
print r.status_code
print r.text
I was able to figure out what was missing in my request using a request interceptor - chrome extension called "postman"
Turns out that the cookies and headers did not matter at all.
Replacing data=json.dumps(payload) with data=payload fixed the issue.