I'm trying to use the AsyncClient with Starlette app, but this code returns 404 while performing requests to my other, local server at http://localhost:8000 (the other server doesn't register any requests):
app = Starlette(...)
(...)
async with httpx.AsyncClient(app=app) as client:
response = await client.post("http://localhost:8000/graphql/", json=data, headers=headers)
while this code works:
response = httpx.post("http://localhost:8000/graphql/", json=data, headers=headers)
I'd expect the difference is because you've got a trailing slash in the first case, but not in the second. Right?
Incidentally Starlette really ought to be issuing a redirect in that case. It's not doing so due to a bug that is resolved in https://github.com/encode/starlette/pull/812 which is in master but hasn't been pushed to a new release yet.
The missing / is only an issue of copy-pasting and truncating the code that causes the error. All the url, data and headers are the same in both cases. Plus in the case of AsyncClient the target server doesn't register any requests (it can basically can be down).
I'm not able to reproduce this...
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.graphql import GraphQLApp
import asyncio
import graphene
import httpx
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
def resolve_hello(self, info, name):
return "Hello " + name
routes = [
Route('/graphql/', GraphQLApp(schema=graphene.Schema(query=Query)))
]
app = Starlette(routes=routes)
async def main():
async with httpx.AsyncClient(app=app) as client:
data = {
'query': '{hello(name: "stranger")}',
'variables': None
}
response = await client.post("http://localhost:8000/graphql/", json=data)
print(response)
print(response.json())
asyncio.run(main())
Then...
$ python ./example.py
<Response [200 OK]>
{'data': {'hello': 'Hello stranger'}, 'errors': None}
I'm using uvicorn.
runserver.py:
uvicorn.run(
"project.app:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG,
)
I'm not clear what the issue is here, the most helpful thing you could do is to reduce this down to a reproducible copy+pastable example that demonstrates the problem.
When you mention "the other server doesn't register any requests", I think there's some confusion there, since you're not making actual network requests when you use httpx.AsyncClient(app=app), instead you're making requests directly to the application itself. (You don't need to be running it in a seperate server.)
I want to make a request to the external server. I must have misread the documentation and thought that the app kwarg was used to pass the loop. Previously I was testing http3 lib that threw "cannot instantiate loop inside loop". When I've looked into httpx docs and found out that you can pass the Starlette's app into the AsyncClient I thought it was for that reason.
What kept me from realising my error is the fact that you still need to provide the url while the client is plugged into the app. But from what I can see it ignores the protocol, host and port parts of it? Perhaps it could expect only the path (and query, fragment etc.) while the client is plugged into an application?
What kept me from realising my error is the fact that you still need to provide the url while the client is plugged into the app.
Didn’t get to answer before but I was indeed under the impression that that part got you confused — you weren’t calling into localhost, but really into the app. :-)
Maybe we could default the base_url to http://testserver. But that would be a bit messy, and possibly backwards incompatible. Instead, a more low-touch approach would be documenting to use that base_url when setting up an app-based client? (That is, updating our example code snippets.)
updating our example code snippets
Always good to update the docs!
or get a warning if app is used and base_url not set ? if that makes sense,
maybe it's used in other parts so maybe stupid >:_
On Thu, Jan 30, 2020 at 4:38 PM Stephen Brown II notifications@github.com
wrote:
updating our example code snippets
Always good to update the docs!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/encode/httpx/issues/798?email_source=notifications&email_token=AAINSPS52AYOSE5IJ2PBMSDRALYAXA5CNFSM4KNUJFJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKLNVNI#issuecomment-580311733,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAINSPSBV5MKFK247VXWPPDRALYAXANCNFSM4KNUJFJQ
.
--
benoit barthelet
http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE