0

Does someone know if it's possible to create documents programmatically with couchdb via an ajax request? I don't find anything about this on the doc, nor on the forums... Any help or hint will be appreciated...

jfgouhier
  • 1
  • 3
  • Please have a look to the following answer that explains how it can be done using Angular's HttpClient class: https://stackoverflow.com/a/56574346/2358409. It should be similar with ajax. – uminder Jun 14 '19 at 03:44
  • Thanks for your answer and your solution (https://docs.couchdb.org/en/2.3.1/api/database/bulk-api.html#db-bulk-docs) - with a POST request this time (and the possibility to create one or more document in the same request if I understand). – jfgouhier Jun 14 '19 at 06:20

1 Answers1

0

I found the solution which is something like that :

  $.ajax({
    url: "http://localhost:5984/mybase/anewid",
    type: "PUT",
    data: {  JSON.stringify("name": "joe", "mykey2": "myvalue2")},
    dataType: "json",
    success: function (result) {
        ...
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
    }
});

without the JSON.stringify encapsulation you have an error 400 :{"error":"bad_request","reason":"invalid UTF-8 JSON"}

ref in couched documentation : http://docs.couchdb.org/en/2.2.0/api/document/common.html#put--db-docid

jfgouhier
  • 1
  • 3