1

I am using a HTTPx client.

See this documentation for wrapping a FastAPI app in a HTTPx Async Client. https://fastapi.tiangolo.com/advanced/async-tests/#example

It is just, not with block, I return the AsyncClient directly.

Everything feels fine. I can just ping my registered endpoints like:

client.post("/some/thing", json=some_thing.dict()

And it works wonderful! I can send form data with data parameter, or body data with json parameter.

Problem booms at when I try to test an endpoint that takes a file.

I was initially getting boundary error, which I could avoid by giving a boundary by myself, but I just deleted the header entirely, and it was fine. alright. https://stackoverflow.com/a/17438575/11210214

and now I am getting error processing the body error. Oh, no.

My endpoint and the error really looks similar to this dude implemented: https://github.com/tiangolo/fastapi/issues/2401#issue-749610783

But my problem isn't with Postman, it is with HTTPx Async Client! He gets the error reading the file before the endpoint executes, so do I. Only difference is, he nukes the file from Postman, and I do it with HTTPx Async Client.

And then I checked "cansu, are we giving the correct key?" and, yes, I do!

https://github.com/tiangolo/fastapi/issues/1536#issuecomment-640781718

My endpoint takes, lets say, a data_file = UploadFile() and, indeed, as the above issue comment guided me, I wrote it correctly. Changed files to data_file and such sort of stuff.

So, in the end, file is going to the endpoint. Form is going to the endpoint. And file can't get processed,while form is just, there.

Everyone gets boundary errors, like https://stackoverflow.com/a/67777054/11210214

https://stackoverflow.com/a/74170335/11210214

And here I am endpoint can't take an image, that is coming from Async Client! Urgh.

Deleted the headers.

Set the header (multipart-form/data) and boundary myself by calculating length of the file and form data.

Tried to use the TestClient of FastAPI, and it just straight doesn't work when pinging asynchronous endpoints saying "no current event loop". I hate testing FastAPI.

Sent many other different file types other from a simple image. Can't process a single one.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • I am going to sleep. My code can be replicated with wrapping the FastAPI app in this issue https://github.com/tiangolo/fastapi/issues/2401#issue-749610783 in a HTTPx Async Client and sending file to it. Good night ladies and gentlemen, I hope I will wake up to a better day than today. – cansusargin Apr 18 '23 at 22:43
  • 1
    Please avoid profanity here – ChrisGPT was on strike Apr 19 '23 at 06:03
  • @Chris that would require changes to endpoint instead of changing only the test code right? Like I have to drop UploadFile from parameters, and get the file from request by myself. – cansusargin Apr 19 '23 at 06:16
  • Do you have any code that actually show the problem and how you've written it? It's hard to try to solve something when you're not sharing code that shows what you're actually doing and what you're trying to get to work. – MatsLindh Apr 19 '23 at 07:59

0 Answers0