I am trying to use Axios to perform a post to a local couchdb database.
const options = {
headers: {
'Content-type': 'multipart/form-data',
Referer: "http://localhost:5984",
Accept: "application/json"
}
}
try{
const response = await axios.get("http://Username:Password@localhost:5984/users/" + user.Name + "/")
rev = response.data._rev
console.log("Fresh _rev acquired.\n\n")
}catch{
console.log("An error occured")
}
try{
var response = await axios.post('http://Username:Password@localhost:5984/users/' + user.Name + '/?_rev="' + rev + '"', user, options)
console.log("posted\n\n")
}catch(error){
console.log("post failed\n", error)
}
This produces an error message. "Request failed with status code 500" The data line of the error message is: data: { error: 'case_clause', reason: 'undefined', ref: 2200284843 } After researching the problem I believe the error is caused by axios using multipart/form-data rather than application/json, but when I change that I get errors stating that the content type must be multipart/form-data. I have found a work around for using the post method by using a get, then deleting the document in the database and finally using a put to make a new document. It is just really bothering me that after a few days of trying to understand the issues I been having with this post method that I am still empty handed. If you have anything that may help, I would greatly appreciate it.
Solved the error 500 by changing the Content-type back to application/json in the header
headers: {
'Content-type': 'application/json',
Referer: "http://localhost:5984",
Accept: "application/json"
}
For this change couchdb returned an error 415. After a bit more digging, and some trial and error I removed the {docid} and revision number from the URL and included them in the data portion of the request.
try{
var response = await axios.post('http://Username:Password@localhost:5984/twitchusers/' /*+ user.Name + '/'?_rev="' + rev + '"'*/, {_id: user.Name, _rev: rev, user}, options)
console.log("posted\n\n")
}catch(error){
console.log("post failed\n", error)
}
This change resulted in no error.