Why playwright isn't modifying request body? - python

I need to modify the request body that is sent by the browser, i do it like this:
async def handle_uri_route(route: Route):
response = await route.fetch()
response_json = await response.json()
reg_notification_uri = response_json['confirmUri']
await page.route(reg_notification_uri, modify_notification_uri)
await route.continue_()
async def modify_notification_uri(route: Route):
post_data = route.request.post_data_json
post_data['notificationUi'] = 'https://httpbin.org/anything'
await route.continue_(post_data=urlencode(post_data))
pprint(route.request.post_data_json)
await page.route(re.compile(r'api\/common\/v1\/register_notification_uri\?'), handle_uri_route)
pprint displays the changed data, but if i open the devtools, then i see that the request hasn't really changed.
What am I doing wrong?

Related

aiohttp session closed without exiting the context manager

I have a pretty complicated API with custom parameters and headers so I created a class to wrap around it. Here's a contrived example:
import asyncio
import aiohttp
# The wrapper class around my API
class MyAPI:
def __init__(self, base_url: str):
self.base_url = base_url
async def send(self, session, method, url) -> aiohttp.ClientResponse:
request_method = getattr(session, method.lower())
full_url = f"{self.base_url}/{url}"
async with request_method(full_url) as response:
return response
async def main():
api = MyAPI("https://httpbin.org")
async with aiohttp.ClientSession() as session:
response = await api.send(session, "GET", "/uuid")
print(response.status) # 200 OK
print(await response.text()) # Exception: Connection closed
asyncio.run(main())
Why is my session closed? I didn't exit the context manager of session.
If I ignore the wrapper class, everything works as expected:
async def main():
async with aiohttp.ClientSession() as session:
async with session.get("https://httpbin.org/uuid") as response:
print(await response.text())
You can't call response.text() once you have left the request_method(full_url) context.
If you write:
async with request_method(full_url) as response:
text = await response.text()
return response.status, text
then the send() method returns without error.

I created a cookies.json file with and used playwright to add cookies to the browser context, but it was always wrong

I created a request using python's requests and saved the cookie of the request response to cookies.json, and used playwright to add cookies to the browser context, but it was always wrong.
playwright._impl._api_types. Error: cookies: expected array, got object
# Save cookie
post = session.post(url, data=data, headers=headers, proxies=proxies)
req = post.cookies.get_dict()
with open('cookies.json', 'w+') as f:
save_cookies = json.dumps(req, ensure_ascii=False)
f.write(save_cookies)
# read cookie
async def worker(browser: Browser, i: int):
cookies = get_csrf_token_login(email_name, password)
context = await browser.new_context()
context.clear_cookies()
with open('cookies.json', 'r') as f:
cookie_test = json.load(f)
await context.add_cookies(cookie_test)
page = await context.new_page()
await page.goto(url)
print(await page.title())
# context
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=False, channel="chrome")
await asyncio.wait(
[asyncio.create_task(worker(browser, i)) for i in range(1)], # 创建上下文数量
return_when=asyncio.ALL_COMPLETED,
)
await browser.close()

Aiohttp adding values to string in discord.py

I am trying to add the user's response into the URL. My code is
async def dungeondata():
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.get('https://sky.shiiyu.moe/api/v2/dungeons/{}/{}'.format(name, cutename)) as resp:
return await resp.json()
#bot.command(name='dungeon', aliases=['dungeons'])
async def dungeon(ctx, name, cutename):
JSONData = await dungeondata(name, cutename)
When the user does ?dungeons , I am trying to add the and to the URL so the url becomes https://sky.shiiyu.moe/api/v2/dungeons/name/cutename. How do I do that?
Add name and cutename as arguments in dungeon function.
async def dungeon(name, cutename):
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.get('https://sky.shiiyu.moe/api/v2/dungeons/{}/{}'.format(name, cutename)) as resp:
return await resp.json()

Async processing of function requests using asyncio

I am trying to achieve aiohttp async processing of requests that have been defined in my class as follows:
class Async():
async def get_service_1(self, zip_code, session):
url = SERVICE1_ENDPOINT.format(zip_code)
response = await session.request('GET', url)
return await response
async def get_service_2(self, zip_code, session):
url = SERVICE2_ENDPOINT.format(zip_code)
response = await session.request('GET', url)
return await response
async def gather(self, zip_code):
async with aiohttp.ClientSession() as session:
return await asyncio.gather(
self.get_service_1(zip_code, session),
self.get_service_2(zip_code, session)
)
def get_async_requests(self, zip_code):
asyncio.set_event_loop(asyncio.SelectorEventLoop())
loop = asyncio.get_event_loop()
results = loop.run_until_complete(self.gather(zip_code))
loop.close()
return results
When running to get the results from the get_async_requests function, i am getting the following error:
TypeError: object ClientResponse can't be used in 'await' expression
Where am i going wrong in the code? Thank you in advance
When you await something like session.response, the I/O starts, but aiohttp returns when it receives the headers; it doesn't want for the response to finish. (This would let you react to a status code without waiting for the entire body of the response.)
You need to await something that does that. If you're expecting a response that contains text, that would be response.text. If you're expecting JSON, that's response.json. This would look something like
response = await session.get(url)
return await response.text()

How can I properly send a POST request to a Website in Pyppeteer

I am attempting to write a bot in pyppeteer.
What I am attempting to do with my code is send a POST request to a website with specific postData
Add_url = f"https://www.website.com/shop/{productID}/add.json"
await page.setExtraHTTPHeaders(headers=headers)
await page.setRequestInterception(True)
page.o
atc_post = await Request.continue_(self,overrides={'url':Add_url, 'method':'POST','postData':data})
print(atc_post.json())
This is my current output from my terminal:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyppeteer/network_manager.py", line 447, in continue_
if not self._allowInterception:
AttributeError: 'Bot' object has no attribute '_allowInterception'
If anybody could help I would greatly appreciate it.
Request.continue_ is only valid after interception for example:
async def intercept (req):
print (req.headers)
await req.continue_ ()
pass
async def main():
browser = await launch()
page = await browser.newPage()
await page.setRequestInterception(True)
page.on ('request', lambda req: asyncio.ensure_future (intercept (req)))
await page.goto('https://www.exemple.com')
await browser.close()
return html
html=asyncio.get_event_loop().run_until_complete(main())

Categories