1

I am using this HTTP POST Request code to send a json data to Couchbase bucket/document. I don't know exactly why it is throwing error as "XMLHttpRequest send Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"

When I tested this, it is working fine through Postman.

var params = {
    "EmpID":"567453",
    "EmpAddress": "XX",
    "EmpAccount": "89723",
    "EmpName": "user1953977",
    "EmpType": "DEV"
}
var xhr = new XMLHttpRequest();
var url = "http://xxxx:PORT/xxxx/xxx/xxx/docs/docs/firstdoc/"; // Couchbase document url
xhr.open("POST", url, true);

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept", "application/json" );
xhr.setRequestHeader("Authorization", "Basic XXXXXXXXXXXXXXXXXX");
xhr.setRequestHeader("Cache-Control", "no-cache");

var data = JSON.stringify(params);
xhr.send(data)

Can someone advise me where is the issue? Is there way a to fix?

user1953977
  • 163
  • 1
  • 11
  • 1
    Any chance that you are correctly AUTHENTICATED when using Postman but not when running your client code? – David Tansey Jan 31 '19 at 22:55
  • 1
    Whatever I'm using "Authorization", "Accept", "Content-Type" through Postman, I'm using it here the same. I don't think that may be the issue. – user1953977 Jan 31 '19 at 23:00
  • Are you making these requests to Couchbase Server or Sync Gateway? Are these requests coming from a browser? – Matthew Groves Feb 01 '19 at 03:20

1 Answers1

1

I'm pretty sure that what you're doing is making POST requests to Couchbase Server from a browser. The 405 error you're getting is likely due to CORS. CORS cannot be turned off in Couchbase Server (unless you fork and compile yourself, I guess). Couchbase Server is not designed to be accessed directly from the browser like this.

Some options:

  1. Couchbase Server sits behind an API and you would use the SDK of your choice (node, Java, .NET, whatever) to interact with Couchbase there.

  2. Use Sync Gateway in front of Couchbase, which has an API that allows your to enable CORS (see Sync Gateway Public REST API documentation).

  3. Turn off same-origin policy in your browser (like in Chrome), but this is not typically practical for most applications, as you would need every user to turn off CORS.

Matthew Groves
  • 22,897
  • 8
  • 64
  • 109
  • 1
    This is good. Another additional issue in this is, when i send this JSON data like this programmatically, it receives as "Binary Document, base64 not available" in couchbase document, instead of receiving the right JSON data. – user1953977 Feb 01 '19 at 05:41
  • The endpoint you're using isn't technically part of the public API. It's not supported, there's no documentation for it. That being said, you might want to have a look at my answer here: https://stackoverflow.com/questions/53124995/adding-document-in-couchbase-and-missing-json-body – Matthew Groves Feb 01 '19 at 14:54
  • 1
    Hello Matthew, I don't know why shouldn't it work through the code? It's just the REST call, since it works fine with Postman. All I need is, it is sending info to CB, but like this "Binary Document, base64 not available". Is it possible to send the right JSON via code or not possible at all? I'm testing it for now, not the final code. Finally, I'll plan to use SDK's only. – user1953977 Feb 05 '19 at 06:48
  • That question is asking the exactly same thing, and my answer addresses it exactly. You need to pass your JSON document as a form encoded value in a field called "value". You aren't doing that in your JavaScript (which won't work anyway because of CORS), and I can only assume you aren't doing it in Postman either. Again, I must stress, this API is not supported, it may change/break/disappear from release to release. – Matthew Groves Feb 05 '19 at 14:42