httpx icon indicating copy to clipboard operation
httpx copied to clipboard

feat(content): allow multipart requests without files.

Open laipz8200 opened this issue 1 year ago • 7 comments

Summary

related #3396.

Currently, a multipart request can only be sent when the files parameter is non-empty. This restriction limits cases where users might want to send data using multipart mode without attaching any files, which is possible in tools like Postman.

Change: Instead of requiring files to be non-empty, we could determine the need for a multipart request based on whether files is None rather than its emptiness. This would allow users to send multipart requests without attaching files if needed.

I think there is no significant documentation update required, so I have not made any changes. However, please let me know if any updates are needed.

Checklist

  • [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [x] I've updated the documentation accordingly.

laipz8200 avatar Nov 08 '24 06:11 laipz8200

Thanks. I've updated the tests to avoid private imports.

Do we think this is an improved behaviour? It does seem nice that an explicit files={} will enforce multipart. (On the other hand, if data and files are determined dynamically as in the cli we need to explicitly switch to None)

lovelydinosaur avatar Nov 15 '24 11:11 lovelydinosaur

Thanks for the update!

My original plan was to make it easier to use multipart without having to carry files, which wasn’t as straightforward before.

I don’t use the cli much, so if this causes any issues, just let me know.

laipz8200 avatar Nov 16 '24 17:11 laipz8200

@laipz8200 @tomchristie When do you plan to merge this feature? I am running into a similar issue

ujayant avatar Jun 01 '25 20:06 ujayant

@tomchristie @laipz8200 Is there a workaround for this in the older versions?

ujayant avatar Jun 01 '25 20:06 ujayant

Is there a workaround for this in the older versions?

@ujayant Here is a workaround: https://github.com/encode/httpx/issues/3396#issuecomment-2508142475. I apologize for not being able to dedicate time recently to address this issue.

laipz8200 avatar Jun 11 '25 08:06 laipz8200

Is there a workaround for this in the older versions?

@ujayant Here is a workaround: #3396 (comment). I apologize for not being able to dedicate time recently to address this issue.

I see this error when I try to use your example - chunk = self.file.read(self.CHUNK_SIZE) AttributeError: 'dict' object has no attribute 'read'. You will have to pass an empty file

ujayant avatar Jun 11 '25 11:06 ujayant

Hi @tomchristie,

Sorry for the late reply — I think this PR is ready to be merged.

Regarding the point @sylann raised, I see it as more of a decision question: do we want to offer the same functionality for the CLI as well? If yes, should it be done in this PR?

laipz8200 avatar Aug 14 '25 19:08 laipz8200