1

I am trying to create a service where a user will be able to fill out a form(all string data) and upload a file with it. For the frontend we are using Angular 8 and for the backend we are using Java. We have it set up using JBOSS but I don't know if that makes a difference here. When trying to submit we are getting a few errors that we are unable to fix. After doing many trial and error tests we are unsure of what to do.

  • Frontend method frontend method
  • Method that calls independentBusiness method call
  • Frontend file

file

  • Backend endpoint backend endpoint

When we leave the content type as undefined(no options specified in post method) we get: "415 Unsupported media type content-type application/octet-stream not supported" 415 error

If we set the content type specifically to undefined via options, it says cannot read property "length" of undefined

When we set the content type to multipart/form-data we get: "Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found" We've tried to set a boundary with it as well but it doesn't seem to work.

The method being used multiOptions and the error boundary not set Any suggestions would be greatly appreciated. If you need any other info let me know and I'll provide it.

Thanks!

GMcCarty
  • 21
  • 5
  • I can't see the code calling independentBusiness. I suppose FormData is a javascript object so it might not match the request params of the API – Benjamin Caure May 13 '20 at 20:44
  • @BenjaminCaure I added a picture with the code that calls independentBusiness, thanks for looking into this. – GMcCarty May 13 '20 at 20:55
  • I'm quite sure that Spring does not know how to build the "IndependantBusinessForm" object from the request param, as Spring doesn't know it is JSON content. Can you try to change the formIn param to a basic "Object" type? – Benjamin Caure May 14 '20 at 07:27
  • Yeah we changed it to try to accept a string and file or even an int and a file and neither worked. It could be a configuration thing but we are looking into it. Thanks for your help on this. – GMcCarty May 15 '20 at 18:34

2 Answers2

0

I am unable to see you using the HttpHeaders in your code from the images you have provided.

Include an HttpOptions object within the request like so:

var requestHeaders = new HttpHeaders();
requestHeaders.append('<key>', '<media type>');

const httpOptions = {
  headers: requestHeaders
};

this.http.post<any>(url, body, httpOptions).subscribe()

Post Request Angular Docs

Harry
  • 2,470
  • 4
  • 30
  • 52
  • That's a good point, I forgot to include those above when I made this. I have updated the images above but we are still getting the same errors. – GMcCarty May 13 '20 at 21:18
  • Have you tried removing the boundary? and allowing the browsers to fill that information in? – Harry May 13 '20 at 21:20
  • yes, it still gives us a no boundary found error. I saw somewhere that it will auto generate the boundary for us and even when we specify a boundary like above with BoundaryHere it is not BoundaryHere it is --WebKit7132764 or something like that – GMcCarty May 13 '20 at 21:26
  • 1
    the only other thing I can think of is to test this api call directly via Postman and see if it reaches your api? because the angular side looks to be ok – Harry May 13 '20 at 21:28
  • 1
    https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data the WebKit... is a browser generated value part of the boundary learned something new :) – Harry May 13 '20 at 21:30
  • 1
    Yeah I can try that, I've never used Postman but it's worth a shot. Thanks for your help! – GMcCarty May 13 '20 at 21:31
0

Just as a solution to our issues with multipart, we ended up just attaching the file in the JSON with fileName, mimeType, and contentBase64Encoded and sending it all as one string. We were unable to get multipart to work and this was the solution for us. Posting this just in case someone else runs into the same issue.

GMcCarty
  • 21
  • 5