question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting cookies in AIOHttpTransport doesn't work

See original GitHub issue

I’m trying to set cookies in the transport to execute a query. This works when I use RequestsHTTPTransport but not when I use AIOHTTPTransport. The cookie needs to be present in the POST request. In case it’s not present, the client gets redirected to the login page. So if I’m using requests library directly, it should look like this:

cookies = authenticate(url, username, password)

json = { 'query' : '{ queryName { id }}' }

r = requests.post(url, json=json, cookies=cookies)

This works:

from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.requests import RequestsHTTPTransport

cookies=authenticate(url, username, password).get_dict()
transport = RequestsHTTPTransport(url=url, timeout=900, cookies=cookies)

with Client(
    transport=transport,
#     fetch_schema_from_transport=True,
    execute_timeout=1200,
) as client:
    query = gql('''
        query {
            queryName { id } 
        }
    ''')
    result = client.execute(query)
    print(result)

This redirects me to the login page.

from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport

cookies=authenticate(url, username, password).get_dict()
transport = AIOHTTPTransport(url=url, timeout=900, cookies=cookies)

async with Client(
    transport=transport,
#     fetch_schema_from_transport=True,
    execute_timeout=1200,
) as client:
    query = gql('''
        query {
            queryName { id } 
        }
    ''')
    result = await client.execute(query)
    print(result)

I need to send the same cookies for uploading a file via graphQL which is why I need to use AIOHttpTransport. How do I send cookies the same way as in requests?

Note: Tried sending via extra_args but that didn’t work.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
Izaydacommented, Apr 15, 2021

Sorry, posted previous message without page reload.

Thank you very much, you save my day, thats works perfect!

As i can see, there are some unexpected features in aiohttp with SimpleCookie, because sometimes cookies are enclosed with quotes e.t.c. There are some issues in aiohttp repo https://github.com/aio-libs/aiohttp/issues/5397 So, i guess, authors problem is also caused by using cookies without CookieJar.

0reactions
leszekhanuszcommented, Apr 24, 2021

I added some documentation about this in PR #202

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting cookies in AIOHTTPTransport doesn't work
I'm trying to set cookies in the transport to execute a query using gql. This works when I use RequestsHTTPTransport but not when...
Read more >
AIOHTTPTransport — gql 3 3.0.0 documentation
Using HTTP Cookies. You can manually set the cookies which will be sent with each connection: transport = AIOHTTPTransport(url=url, cookies={"cookie1": ...
Read more >
Cookie not shown/stored in the browser - Help - Apollo GraphQL
My problem with it is that in apollo studio my cookie, which is created inside the UserResolver under the mutation login, isn't shown...
Read more >
Use Neptune graph notebooks to get started quickly
You don't have to use Neptune graph notebooks to work with a Neptune graph, ... You can use the Neptune console to set...
Read more >
GraphQL API - Ghostwriter
Unlike a REST API, a GraphQL API does not have specific endpoints you must query with a particular HTTP request type to receive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found