A memo that was soberly addicted to the request of multipart / form-data

multipart/form-data The other day, I implemented communication processing using Java's HttpURLConnection. Among them, I made a part that can be sent universally in the multipart / form-data format, but for some reason it is not recognized as a multipart / form-data request no matter how many times it communicates, and it takes a considerable amount of time to resolve it. I have spent it.

The cause is very ridiculous, is there anyone else who is addicted to this? It was like that, but I would like to write it as a memorandum.

By the way, when I wrote it first, it was the cause. imageのコピー2.jpg

Request body for multipart / form-data

The general one is like this. The contents assembled in the following format are put on the Body and communicated.

multipart/form-body of data


--[Boundary string]
Content-Disposition: form-data; name="[Form name]"

<<Form contents>>
--[Boundary string]
Content-Disposition: form-data; name="[Form name]"; filename="[file name]"
Content-Type: text/plain

<<File contents>>
--[Boundary string]
・
・
・
Continues as much as you send
・
・
・
--[Boundary string]--

Write the Content-Type of the request header after multipart / form-data and the Boundary string after the Content-Type. The line feed code must be CRLF.

For some reason it is not recognized ...

Looking at the request body of the sent request, there is no sign of misunderstanding compared to the above format, and there is no doubt that the Boundary specification of Content-Type in the request header is correct. Hmm?

Cause

If you communicate from Postman in form data format and compare it with the request that is recognized normally,

multipart/form-body of data


--[Boundary string]

There was no first "-". .. At first, I was not very familiar with the mechanism of multipart / form-data, so I made it by referring to the sites on the net, but on various sites, as a Boundary character string, "-------- random character string" Because it was a specification like this, I learned from it and specified the character string with "--------" at the beginning, and there was a lack of recognition that a separate "-" was required at the beginning. The cause was that it was difficult to confirm whether or not it was actually removed. (It's a story to read RFC properly.)

By the way, the last delimiter needs a "-" after it! (Reminder)

Lesson

The belief is scary.

Write it down so that no one gets stuck with similar content. (Is there such a person?)

By the way

By the way, the following sites are recommended when testing HTTP communication. There are endpoints that return information about requests sent by GET or POST in the form of JSON, binary responses, and various other endpoints, so simple communication You can also test the communication fairly firmly from the confirmation of the contents.

If you don't know it, please use it. Make progress. httpBin: https://httpbin.org/

Recommended Posts

A memo that was soberly addicted to the request of multipart / form-data
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I was addicted to the record of the associated model
A memo that I was addicted to when making batch processing with Spring Boot
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
I was addicted to the setting of laradock + VSCode + xdebug
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
A memo that containerizes the simple chat application of Node.js + socket.io
About the matter that I was addicted to how to use hashmap
I was addicted to the API version min23 setting of registerTorchCallback
I was addicted to the roll method
I was addicted to the Spring-Batch test
A memo when you want to clear the time part of the calendar
A story I was addicted to when testing the API using MockMVC
I was addicted to a simple test of Jedis (Java-> Redis library)
Recorded because I was addicted to the standard input of the Scanner class
A story that was embarrassing to give anison file to the production environment
The story of making it possible to build a project that was built by Maven with Ant
A story addicted to EntityNotFoundException of getOne of JpaRepository
Make a margin to the left of the TextField
Set the time of LocalDateTime to a specific time
I was a little addicted to the S3 Checksum comparison, so I made a note.
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android
A memo about the types of Java O/R mappers and how to select them
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
A story I was addicted to when getting a key that was automatically tried on MyBatis
[Java] The problem that true was returned as a result of comparing Integer with ==
I was addicted to the NoSuchMethodError in Cloud Endpoints
GetXxxx of ResultSet was addicted to primitive type (Java)
Addicted to the webpacker that comes standard with Rails 6
Memo to silently write the source that seems to be usable and the result of compilation execution-java
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
What I was addicted to when introducing the JNI library
Determine that the value is a multiple of 〇 in Ruby
I was addicted to looping the Update statement on MyBatis
Technical causes and countermeasures for the points that I was addicted to with the Android app & Kotlin (2. Processing related to the camera function of Android *)
A story I was addicted to in Rails validation settings
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
[Ruby] How to retrieve the contents of a double hash
What I was addicted to with the Redmine REST API
A program that counts the number of words in a List
[jsoup] How to get the full amount of a document
The story I was addicted to when setting up STS
A collection of patterns that you want to be aware of so as not to complicate the code
The story of making a binding for libui, a GUI library for Ruby that is easy to install
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
A memo that was unexpectedly difficult even though it seemed easy to exchange between two Entity
I made a GitHub Action that makes it easy to understand the execution result of RSpec
[Past POST] I will publish a part of the answer memo when I was a mentor of TECH :: CAMP
[Circle CI] A story I was addicted to at Start Building
Try to imitate the idea of a two-dimensional array with a one-dimensional array
Mechanism for converting to a language that the browser can recognize
A solution to the problem of blank spaces at the beginning of textarea
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
[Ruby 3.0] A memo that I added a type definition to a library I wrote