Skip to content

Conversation

rdimartino
Copy link
Contributor

Fixes #821

irb(main):003> HTTParty.post('http://localhost:8888', body: { user: { name: "John Doe", avatar: File.open(path) } })
=> #<HTTParty::Response:0xb070 parsed_response=nil, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-length" => ["0"]}>
irb(main):004> HTTParty.post('http://localhost:8888', body: { user: { name: "John Doe", avatar: File.open(path, 'rb') } })
=> #<HTTParty::Response:0xf0f0 parsed_response=nil, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-length" => ["0"]}>
irb(main):005> HTTParty.post('http://localhost:8888', body: { user: { name: "Jöhñ Ďœ", avator: File.open(path) } })
=> #<HTTParty::Response:0x10ca0 parsed_response=nil, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-length" => ["0"]}>
irb(main):006> HTTParty.post('http://localhost:8888', body: { user: { name: "Jöhñ Ďœ", avator: File.open(path, 'rb') } })
=> #<HTTParty::Response:0x14d20 parsed_response=nil, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-length" => ["0"]}>

I think this is safe because String#force_encoding says it "does not change the underlying bytes". Also the default mode for Files in Ruby is text data which sets the default external encoding to Encoding::UTF_8.

@jnunemaker jnunemaker self-assigned this Oct 7, 2025
@jnunemaker
Copy link
Owner

First, I don't know much about multi part or encoding. Haha. So I'm glad you fixed and added test.

Thanks! Confirmed that the fix works. I do think there are still holes where mismatches could happen but this fixes one that several people have reported so lets give it a whirl.

@jnunemaker jnunemaker merged commit 6e1e869 into jnunemaker:main Oct 7, 2025
6 checks passed
@rdimartino
Copy link
Contributor Author

Thanks for the speedy review!

@rdimartino rdimartino deleted the rd/fix-821 branch October 8, 2025 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multipart POST fails with files opened in binary mode and other fields including non-ASCII characters

2 participants