1

I'm using Spring Boot with OpenFeign and I'm trying to upload a file to Backblaze B2.

I already have b2_authorize_account and the b2_get_upload_url endpoints working. Now I'm trying to send a request to b2_upload_file.

The code I have for the Feign client request is

@PostMapping()
String b2UploadFile(URI uri,
                    @RequestHeader("Authorization")     String uploadUrlToken,
                    @RequestHeader("X-Bz-File-Name")    String fileName,
                    @RequestHeader("Content/Type")      String contentType,
                    @RequestHeader("X-Bz-Content-Sha1") String contentSha1,
                    byte[] file);

When I execute the code I get a java.net.SocketException: Connection reset exception. I think that the server is closing the connection.

I tested the endpoint using Postman and it works, I was able to upload my file.

I applied the same headers and values, the difference between Postman and OpenFeign is that in Postman I clicked Body -> binary -> upload file where as in OpenFeign I am passing the file using byte[] without any annotations.

In the API documentation it also says I need the Content-Length header, however when I include it in OpenFeign I am getting a number format exception. I was unable to resolve it but based on what I have read OpenFeign should do this automatically. I also did not include the header in Postman and it still works as Postman also includes it by default so I don't think that's the issue.

I have tried annotating the file with @RequestBody byte[] file, making it a multipart file, using the file type, using string type, using outputstream and inputstream.

The API says:

There are no JSON parameters allowed. The file to be uploaded is the message body and is not encoded in any way. It is not URL encoded. It is not MIME encoded.

metadaddy
  • 4,234
  • 1
  • 22
  • 46

1 Answers1

0

From discussion in the comments

OpenFeign doesn't let you set the Content-Type header directly, so you have to specify:

@PostMapping(consumes = "b2/x-auto", produces = "application/json")

Original Answer:

In the code you posted,

@RequestHeader("Content/Type")      String contentType,

should be

@RequestHeader("Content-Type")      String contentType,
metadaddy
  • 4,234
  • 1
  • 22
  • 46
  • I had Content-Type originally but I kept getting the error `Invalid mime type "{Content-Type}": does not contain '/'` The value passed into comment-type did contain a / so the only other thing I could think of was replacing - with / in content-type and the error disappeared. – user19201263 May 26 '22 at 14:01
  • 1
    After looking at your suggestion I went back and investigated Content-Type and it turns out that replacing it with Accept instead did the trick! thanks. Edit: The upload is working but it seems like Backblaze is now not picking up the correct content type and defaulting to "application/octet-stream" – user19201263 May 26 '22 at 14:29
  • That's odd. Accept is what you'd like to see in the response, which would be `application/json`. What Content-Type are you setting now? What sort of data are you uploading? Does the `X-Bz-File-Name` filename have an extension? – metadaddy May 26 '22 at 15:01
  • 1
    I removed Accept so now I only have three headers `Authorization` `X-Bz-File-Name` and `X-Bz-Content-Sha1` and it still uploads. I think open feign put the content type automatically and ignored the Accept when I passed it in. I'm uploading a PNG file and the file name does contain the .png extension. The file is sent to the controller as a Multipart file which I then do the .getBytes() on it. I also tried saving it first, loading it via BufferedImage and getting the bytes that way. I tried `value = HttpHeaders.CONTENT_TYPE` for the header and passed in `image/png` but still get the error – user19201263 May 26 '22 at 15:36
  • Sounds like it's working now, but one way to figure out what's going on in situations like these is to create a Request Bin at https://requestbin.com/ (you can create a public bin without setting up an account) and use the bin URL as the upload URL. You'll be able to see exactly what Open Feign is putting on the wire. – metadaddy May 26 '22 at 19:24
  • 1
    Awesome thanks for the site! I saw that I was still getting the Content-Type error using the site you sent me so I went back and added `@PostMapping(consumes = "b2/x-auto", produces = "application/json")` . Now the upload is working and the correct content type is set on backblaze based on the extension. Seems there is something up with open feign when you use Content-Type directly. Thanks again! – user19201263 May 26 '22 at 21:50
  • @user19201263 - I edited my answer to include the solution you found. Would you mind marking it as correct and upvoting it so it's clear to others that it solves the problem? Thanks! – metadaddy Jul 11 '22 at 17:14