Error when editing or creating items through CLI

I keep getting “Error parsing the encoded request data.” no matter what I tried:

I have tried created new json file, encoded it and tried to create an item. Then I kept getting the error “Error parsing the encoded request data.”.
Thinking the error is from incorrect json template,I created a login item through the gui and then I tried working with that to start from a working example.
I manually modifiied the json file from get item output of the working example , encoded it and tried editing the existing item. At a last attempt, I encoded the unchanged output from a get item command of the working example and edited the item with the encoded json file, but every time again, I get the same error: "Error parsing the encoded request data.
I am past the point where Im thinking it is probably a syntax error in the JSON file, but what else could it be?

@Peterh2 Welcome to the forum!

I added “(CLI)” to your title, it may make it a bit more clear from the beginning, that you are talking specifically about the CLI… Please tell me, if you are not okay with that new title.

yup, that fine. Thank you for that.

1 Like

@Peterh2 Welcome to the forum!

It may help if you post the exact syntax of the CLI commands you are trying.

When posting CLI commands in your response here, please place the cursor on a new line in the comment editor, and then click the “pre-formatted text” button </> in the top menu.

I think the issue may be related to MS-DOS command line, as the error appears to be persisting only when I run bw commands in that shell.
When I use powershell, it works fine.

I have only just started with this, so if anyone with more expreience could give me some tips, I would appreciate it. Maybe there is a better, shorter way to accomplish the same? Also, at this time I cannot use this script for automation, as I need to manually add the password, when prompted. Is there any way to handle this better?

Thanks!

Here are the scripts that I tested:
PowerShell

PS C:\Users\ph>$item_id = bw list items --search "goodexample" | jq -r '.[0].id'
? Master password: [hidden]
PS C:\Users\ph>bw get item $item_id > goodexample.json
? Master password: [hidden]
PS C:\Users\ph>$jsonContent = Get-Content -Raw -Path goodexample.json | ConvertFrom-Json
PS C:\Users\ph>$jsonContent.login.password = "thisistheworstone"
PS C:\Users\ph>$jsonContent | ConvertTo-Json -Depth 10 | Set-Content -Path goodexample.json
PS C:\Users\ph>$encodedJson = echo (Get-Content -Raw -Path goodexample.json) | bw encode
PS C:\Users\ph>bw edit item $item_id $encodedJson
? Master password: [hidden]
{"passwordHistory":[{"lastUsedDate":"2025-01-01T17:50:55.367Z","password":"bestpwdever"}],"revisionDate":"2025-01-01T17:50:53.917Z","creationDate":"2025-01-01T11:56:48.990Z","deletedDate":null,"object":"item","id":"0d204273-5f35-4147-orite":false,"login":{"fido2Credentials":[],"uris":[{"match":null,"uri":"good-prob-01"}],"username":"gooduser","passw
PS C:\Users\ph>bw get item $item_id
? Master password: [hidden]
17:50:53.917Z","creationDate":"2025-01-01T11:56:48.990Z","deletedDate":null,"object":"item","id":"0d204273-5f35-4147-b482-b25800c4e13c","organizationId":null,"folderId":null,"type":1,"reprompt":0,"name":"goodexample","notes":null,"favord":"thisistheworstone","totp":null,"passwordRevisionDate":"2025-01-01T17:50:55.367Z"},"collectionIds":[]}
PS C:\Users\ph>

MS-DOS:

C:\Users\ph>bw get item %item_id% > goodexample.json
? Master password: [hidden]

C:\Users\ph>jq ".login.password = \"thisistheworstoneyet\"" goodexample.json > updated_goodexample.json

C:\Users\ph>bw encode < updated_goodexample.json > encoded.json

C:\Users\ph>bw edit item %item_id% encoded.json
? Master password: [hidden]
Error parsing the encoded request data.
C:\Users\ph>type updated_goodexample.json
{
  "passwordHistory": [
    {
      "lastUsedDate": "2025-01-01T18:02:42.315Z",
      "password": "thisistheworstone"
    },
    {
      "lastUsedDate": "2025-01-01T17:50:55.367Z",
      "password": "bestpwdever"
    }
  ],
  "revisionDate": "2025-01-01T18:02:40.876Z",
  "creationDate": "2025-01-01T11:56:48.990Z",
  "deletedDate": null,
  "object": "item",
  "id": "0d204273-5f35-4147-b482-b25800c4e13c",
  "organizationId": null,
  "folderId": null,
  "type": 1,
  "reprompt": 0,
  "name": "goodexample",
  "notes": null,
  "favorite": false,
  "login": {
    "fido2Credentials": [],
    "uris": [
      {
        "match": null,
        "uri": "good-prob-01"
      }
    ],
    "username": "gooduser",
    "password": "thisistheworstoneyet",
    "totp": null,
    "passwordRevisionDate": "2025-01-01T18:02:42.315Z"
  },
  "collectionIds": []
}

C:\Users\ph>

You have to pass the encoded json object to bw edit item either as an argument or from stdin.

But you are doing neither, you are passing a file name as an argument. And bw edit item is not reading it. That’s what’s wrong.

Try changing the line I quoted to:

bw edit item %item_id% < encoded.json

By the way, on the powershell script you are passing the encoded json object as an argument (in a variable) that’s why it’s working there.

Both scripts do two very different things: assigning the encoded json object to a variable vs. writing it to a file. That’s why the first one is working and the second one is not.

2 Likes

Thanks for that solution and taking the extra time to explain my errors.

I did find that while your solution removes the error, it doesn’t allow the edit command to complete successfully, though: It would not allow me to enter the password when prompted end entered a carriage return instead, for some reason.

I have tried using the --session option and luckily that removes the need to enter a password when editing the item.

@echo off
:: Prompt for the item name
set /p ITEM_NAME=Enter the name of the item: 

:: Run bw unlock --raw and manually enter the password
for /f "tokens=*" %%i in ('bw unlock --raw') do set SESSIONKEY=%%i

:: Search for the item by name and capture the output
for /f "delims=" %%i in ('bw get item "%ITEM_NAME%" --session %SESSIONKEY% ^| jq -r ".id"') do set item_id=%%i

:: Retrieve the item by ID and save it to currentitem.json
bw get item %item_id% --session %SESSIONKEY% > currentitem.json

:: Update the password in the JSON file
jq ".login.password = \"noneedforapassword\"" currentitem.json > updateditem.json

:: Encode the updated JSON file
bw encode --session %SESSIONKEY% < updateditem.json > encoded.json

:: Edit the item with the new encoded JSON data
bw edit item %item_id% --session %SESSIONKEY% < encoded.json