REST Put binary content

REST Put binary content

  

Hi all,


I need to call a put operation of an API in order to upload a binary file.


Using js this should be implemented as below. 

Is it possible to build this using the OS out of the box rest integration? If yes, how can the raw binary data be set in the request?



function sendFile(e) {
    e.preventDefault();

    // get the reference to the actual file in the input
    var theFormFile = $('#theFile').get()[0].files[0];

    $.ajax({
      type: 'PUT',
      url: "<URL>",
      contentType: 'binary/octet-stream',
      processData: false,
      // the actual file is sent raw
      data: theFormFile
    })
    .success(function() {
      alert('File uploaded');
    })
    .error(function() {
      alert('File NOT uploaded');
      console.log( arguments);
    });

    return false;
  });


Thank you

Solution

Hi Hugo,

Specify an output parameter of type Binary Data, with Receive In set to "Body". Also set the Response Format of the Method to Binary.

Solution

Kilian Hekhuis wrote:

Hi Hugo,

Specify an output parameter of type Binary Data, with Receive In set to "Body". Also set the Response Format of the Method to Binary.


Thank you Kilian,


I don't need to expose this put operation, I need to consume an operation like that.


I had set up the consume operation to have the Request format as binary and paramater data type as binary data and Send in option as Body




Using these options I'm still not able to consume the api. I'm getting a 400 Bad Request response error.

Hi Hugo,

Are you sure you need to upload binary content, or do you need multipart/form-data? Is the API description available somewhere?

Kilian Hekhuis wrote:

Hi Hugo,

Are you sure you need to upload binary content, or do you need multipart/form-data? Is the API description available somewhere?

Yes, I need to upload the binary. This is a AWS s3 bucket pre signed URL.


One of the tests I did was to update the contentType header to 'binary/octet-stream', but still getting the same error.


I don't have the descritption but I do have an example on how to call it using python:

import os
import requests
file_size = os.path.getsize('./AUDIO-2018-06-01-13-55-33.opus')
url =  '[PRE SIGNED URL]'
headers = {
    'Content-Length': str(file_size),
}
with open('./AUDIO-2018-06-01-13-55-33.opus', 'rb') as data:
  response = requests.put(url, data=data, headers=headers)
  print(response)
  print(response.content)


I'm not very versed in Python, but it seems that indeed it's just the binary content that's sent. Which means that this should do the trick:

(Note I just made up the URL Path, it's about the Request Format.)

Note that when sending Binary Data like this, the Content-Type should already be right.