25

This is slightly different from the question titled "Error about ‘invalid JSON’ with couchDB view but the json’s fine": I am not trying to upload a file only enter a simple document.

The example I am trying to use is actually from O'Reilly's book "CouchDB: The Definitive Guide" and I am pretty sure that I have tried it before and got it to work. Here's the command:

curl -X PUT http://username:password@127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af -d '{"title":"There is Nothing Left to Lose","artist":"Foo Fighters"}'

The database albums exists and the username and password are correct. I have checked this with JSONLint and the JSON is valid and I am at a loss ... presumably there is an issue with the CouchDB server itself but it appears to be running correctly ... any ideas? This is driving me nuts!

Thanks

Alfamale
  • 1,069
  • 1
  • 9
  • 13

3 Answers3

24

Thanks guys. Turns out it's a problem with quote escaping. Here's the answer I got from David on the CouchDB user mailing list:

This is a windows thing regarding quoting - a real PITA. Unfortunately cmd.exe shell on windows doesn't parse this correctly. The rules for when escaping with a "" or a ^" or a \" are a bit vague but this works:

C:\tmp>curl -X PUT http://username:password@127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af -d "{\"title\":\"There is Nothing Left to Lose\",\"artist\":\"Foo Fighters\"}" {"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"1-4b39c2971c9ad54cb37e08fa02fec636"}

C:\tmp>

"basically you need to \"escape\" all \"quotes\" within your JSON"

This fixes my problem

Alfamale
  • 1,069
  • 1
  • 9
  • 13
  • 3
    Is there no other way? I mean, escaping quotes seems like a PITA. – Arunabh Das Feb 10 '13 at 06:34
  • 1
    This is a limitation of the shell. An alternative would be to put the JSON in a file, and use -d @filename – AlexT Aug 12 '13 at 13:18
  • @Das: I solved it by installing Git. It includes Git Bash with curl. – null Sep 05 '14 at 13:41
  • @null yes that is another way... another **better** way. Especially since many of us have git already installed. Plus bash has a better color scheme than cmd ) – ycomp Jun 30 '17 at 06:14
0

I also met same problem, after searching a while, I found about Git Bash in other question. Git bash has curl command inside. The git bash is included in Git installer.

Community
  • 1
  • 1
null
  • 8,669
  • 16
  • 68
  • 98
0

You have to put the escap characterstic ** and also do not forgot to put after -d all things in **"". I.e : curl -X PUT http://127.0.0.1:5984/my_database/"001" -d "{\"Name\":\"Suresh\",\"age\":\"32\",\"Designation\":\"Associates Manager\"}"