Updating cells in smartsheet (java)

481 views Asked by At
>Cell newCell = new Cell();
>newCell.setValue("no way");
>newCell.setColumnId(4303755236665220l);
>       
> connection = (HttpURLConnection) new >URL("https://api.smartsheet.com/1.1/row/{row_id}/cells").openConnection();
>connection.setRequestMethod("PUT");
>connection.addRequestProperty("Authorization", "Bearer " + accessToken);
>connection.addRequestProperty("Content-Type", "application/json");
>connection.setDoOutput(true);
>           
>            
>mapper.writeValue(connection.getOutputStream(), newCell);
>mapper.readValue(connection.getInputStream(), new TypeReference<Result<Cell>>() {});
>               
>System.out.println("cell added.");

This is my Java code to add a cell to the specified row.

It returns an error :-

"Unable to parse request. The following error occurred: Request body must be either a JSON object or JSON array. java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.smartsheet.com/1.1/row/row_id/cells"

row_id has a Long value.

Can anyone help?

1

There are 1 answers

0
stmcallister On BEST ANSWER

It looks like you're using the Update Row Cells endpoint to add values to your cells. When using this endpoint the Smartsheet API is expecting the request body as an array of cells, rather than just a single cell object.

In your case, your request body looks like:

{"columnId": 4303755236665220l, "value": "no way"}

When you want to wrap that object in an array like this:

[{"columnId": 4303755236665220l, "value": "no way"} ]

You could also use the Smartsheet Java SDK. Using the SDK you would only need to make the following calls

 List<Cell> cells = new Cell.UpdateRowCellsBuilder().addCell(4303755236665220l, "no way").build();
 smartsheet.rows().updateCells({row_Id}, cells);

As a side note, this endpoint has been recently deprecated, and we enourage you to use the Modify Row endpoint: PUT /sheet/{sheetId}/row/{rowId}

For this endpoint your request body would only modify slightly, as your array of cells would be wrapped in a row object, like so:

{"cells":[{"columnId": 4303755236665220l, "value": "no way"}]}