javaspringspring-bootopenfeignbackblaze

Uploading a file using the Backblaze B2 API with OpenFeign


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.


Solution

  • 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,