REST API Post Files call.

I am trying to get the /files POST REST call to work.

I am using Postman with the following:

POST

URL: base+/ws-api/v1/files

Header: Token=sessionId from login call

I go to the body tab and select the file in question that I want to upload.

 

The CURL is:

curl -X GET \
baseURL/ws-api/v1/files \
-H 'cache-control: no-cache' \
-H 'postman-token: bd26351d-cf3b-aa17-8c45-70c444203910' \
-H 'token: 1933081325'

 

 

This is the error I get:

{
  "errors": [
    {
      "code": 500,
      "type": "SERVER",
      "message": "Required List parameter 'filename' is not present"
    }
  ]
}

 

 

I know that I'm probably just doing something wrong, I'm just not sure what and how to deal with this error.

  • I did not check on Postman actually, but if you are trying to upload a file by POST, you are using GET since your cURL is showing as "-X GET".
    And I think the token should be a query parameter, not a header parameter.
  • I tried on Postman by myself. If you are trying to upload a file:

    1. Select POST in the URL area.
    2. Specify a URL like, /ws-api/v1/files?token=800262019
    3. In the Body tab, click the form-data option.
    4. Add a new key with the name "file", select a type of the value as 'File', and then specify a file to upload as the value.
    5. Click the Send button.

    You will get a response in JSON, including 'internalName' which can be used for further calls like creating new projects.
    The actual uploaded files will be saved in the temporary folder.
  • Thank you! That seemed to do the trick. Now I can implement that in my code.
  • Getting this code to run in Java was harder than I thought. I got it up and running in Postman with the information provided, but for some reason I couldn't get it working in Java. Since this is inevitabily going to be something that others are going to try here is the code that worked for me.

    private <T> T restPostFileFormData(String sessionId, String endpoint, ContentBody contentBody, Class<T> returnType) {

    try {

    URL url = new URL(<baseURL> + endpoint);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setDoOutput(true);

    connection.setUseCaches(false);

    connection.setRequestMethod("POST");

    connection.addRequestProperty("token", sessionId);

    connection.setRequestProperty("Connection", "Keep-Alive");

    connection.setRequestProperty("Cache-Control", "no-cache");

    String boundary = "--" + UUID.randomUUID() + "--";

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.setBoundary(boundary);

    connection.addRequestProperty("content-type", "multipart/form-data; boundary=" + boundary);

    // Binary file content

    builder.addPart("file", contentBody);

    HttpEntity entity = builder.build();

    entity.writeTo(connection.getOutputStream());

    connection.connect();

    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

    throw new InterchangeException(

    "Status Code: " + connection.getResponseMessage() +

    "\r\nMessage: " + connection.getResponseCode());

    }

    ObjectMapper mapper = new ObjectMapper();

    return mapper.readValue(connection.getInputStream(), returnType);

    } catch (Exception e) {

    LOG.warn(Utils.getDetailedErrorMessage(e));

    }

    return null;

    }

    In order to make this code work you will need to create a Class that is structured like the response. You will also need to correctly input the base URL to get to the application for your needs.