Search code examples
vue.jsrestaxioscouchdbquasar

Post doc to CouchDB from Quasar Vue3 Axios


I have tried a few methods and have not been able to get it to work. The base URL is in the Axios setup file and logging in out it console shows the correct address. Replicating works when using PouchDB but in this case, I didn't want to use Pouchd, there is no need to save local only want to post.

I get this error with this code: " 401 (Unauthorized)" " Error: Request failed with status code 401"

api({
    method: "POST",
    url: "/webdata",
    data: doc,
    headers: { "content-type": "application/json" },
  })
    .then((result) => {
      console.log("result: ", result);
    })
    .catch((e) => {
      console.log("e: ", e);
    });
  

I get this error with this code: "Error on Authentication Error: Request failed with status code 400"

  var username = <user>;
  var password = <pass>;
  var credentials = window.btoa(username + ":" + password);

  var basicAuth = "Basic " + credentials;
  api
    .post("/webdata", {
      headers: { Authorization: basicAuth },
    })
    .then(function (response) {
      console.log("Authenticated ", response);
    })
    .catch(function (error) {
      console.log("Error on Authentication ", error);
    });

It even fails when I use admin user/pass

I also used Postman and it will work if I add id and "/json"

http://<ip>:5984/webdata/<test::id>/json

Thanks for your help


Solution

  • I have found the answer in the syntax:

      api({
        method: "POST",
        url: "/webdata",
        data: doc,
        })
        .then((result) => {
          console.log("result: ", result);
        })
        .catch((e) => {
          console.log("e: ", e);
        });
    

    I removed the "headers: { Authorization: basicAuth }," and "headers: { "content-type": "application/json" }," I had already added it in the axios config. Also, I made sure I had an _id inside the doc.