1

I am trying to understand how HTTP protocol works, So I tried to add headers manually to java Socket to send a request to httpbin.org as shown below:

        BufferedWriter wr = new BufferedWriter(/*socket Outputstream*/)     
        wr.write("POST post HTTP/1.1\r\n");
        wr.write("Host: httpbin.org\r\n");
        wr.write("Accept: */*\r\n");
        wr.write("Content-Length: "+data.length()+"\r\n");
        wr.write("Content-Type: multipart/form-data; boundary=---WebKitFormBoundary67\r\n");
        wr.write("\r\n"); 
        wr.write(data); 
        wr.flush();

In above code data is the payload of HTTP request that looks exactly as below:

   ---WebKitFormBoundary67
   Content-Disposition: form-data; name="field1"
   value1
   ---WebKitFormBoundary67
   Content-Disposition: form-data; name="field2"; filename="example.txt"

   Java is better when it run long
   ---WebKitFormBoundary67--

But the server httpbin.org is not identifying any files attached, am I missing anything?

2
  • I don't see how this shows how http protocol works? Just arbitrary server configurations etc and preferences? Commented Oct 5, 2018 at 0:04
  • 2
    @marshalcraft I think this is what a typical browser does when you submit a form with file-upload in a web page. A protocol is noting but a set of rules two systems agree to communicate without any hassles. Above question has tried to follow rules defined in RFC 2046, so any HTTP server can recognise what kind of information I am sending to it. As I failed to follow the conventions properly the targeting server has failed to identify what data I am exactly trying to send. Commented Oct 5, 2018 at 18:14

1 Answer 1

2

multipart/form-data is a multipart MIME message as defined in RFC 2046. The basic structure of a multipart MIME message in an example of a multipart/form-data message looks like this:

Content-type:  multipart/form-data; boundary=foo

--foo
Content-Disposition: form-data; name=key1

abcde
--foo
Content-Disposition: form-data; name=key2; filename=foo.txt

01234
--foo--

As you can see, the boundary foo is defined in the boundary attribute, is used as delimiter between the parts with --foo and is used as the final boundary as --foo--.

Your code instead defines the boundary not as foo but as --foo and then tries to still use only --foo as a separator between the parts. To correct your code you would either need to set the boundary to only -WebKitFormBoundary67 instead of ---WebKitFormBoundary67, or use -----WebKitFormBoundary67 as separator instead of only ---WebKitFormBoundary67.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I realised that I have to give only -- before the delimiter in the payload to separate the parameters.
Also, in the original data shown, there is a blank line missing between the MIME headers and MIME body for field1, like there is one present for field2. That means value1 gets treated as an unknown MIME header for field1 instead of its text value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.