Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Is it possible to use fetch to access CBL REST endpoint directly? #134

Open
greenais opened this issue Nov 30, 2017 · 7 comments
Open

Is it possible to use fetch to access CBL REST endpoint directly? #134

greenais opened this issue Nov 30, 2017 · 7 comments
Labels

Comments

@greenais
Copy link

Coucbase.init returns url like this:
http://f08e71ac-1065-483c-b18a-4f13e14b1933:19c3571c-4492-4417-a335-844d4e87bad8@localhost:5987

Could those long user:pass@ provided in the url be used to query CBL REST listener?
I tried fetch with

headers: {
     'Authorization': 'Basic '+ base64.encode('user:pass')
  }

but it gets 401 response.
Is there anything specific to consider within CBL REST endpoint authorization?

@jamesnocentini
Copy link
Contributor

Could those long user:pass@ provided in the url be used to query CBL REST listener?

it is intended to be used to query the CBL REST API

'Authorization': 'Basic '+ base64.encode('user:pass')

That's also how the swagger client sends the request https://github.com/couchbaselabs/react-native-couchbase-lite/blob/master/index.js#L16

What version and platform did you try this on? Did you send the request from curl, postman or something else?

@greenais
Copy link
Author

greenais commented Dec 7, 2017

That's exactly what I did from whithin JS code of my RN app - using fetch or XMLHttpRequest with authorization headers properly set. I have no idea why, but url provided by Couchbase.init never worked (I tried plenty of variants), always response 401, that's why I asked for help here.

Just by chance I tried url provided in swagger response - and you know what? It worked like a charm with headers set in exactly the same way as before. So it seems that url provided by Couchbase.init (called in undocumented way) is somehow wrong.

So finally I ended up with the following approach:

  1. initiate manager as described in docs
  2. do the first call to CBL using manager (i.e. mngr.database.get_db({db: 'any_fake_dbname'}))
  3. then use url provided in swagger response as valid one for all following fetch requests

My environment:
latest npm installed RNCBL 0.6.0,
RN 0.50.4,
latest Android simulator

I needed REST API access as swagger .help() docs for query (_design / view) operations is total copypasted html mess unfortunately (as well as all other final endpoints, but there at least was a chance to sort it out looking for analogies in official REST API). With query ones it didn't work for me at all.

@jamesnocentini
Copy link
Contributor

jamesnocentini commented Dec 7, 2017 via email

@greenais
Copy link
Author

greenais commented Dec 7, 2017

Yes, I did, but to my understanding all example todo-apps can't replace one valid and usable documentation.
In example todo-app mentioned doesn't have any examples of using parametrized queries (like with ?key= option) which I needed in my app.
That's why I didn't have other options but switching to official REST API which is at least well-documented.

@jamesnocentini
Copy link
Contributor

Yes, I did, but to my understanding all example todo-apps can't replace one valid and usable documentation.

I've found the swagger client API to help with productivity because it is generated from the swagger spec. I agree, sometimes, the .help() method returns content in HTML which doesn't help. In this case, browsing the swagger UI on developer.couchbase.com is the best option and has exactly the same content.

That's why I didn't have other options but switching to official REST API which is at least well-documented.

The swagger client is generated from the REST API docs. So it's exactly the same but presented differently.

@greenais
Copy link
Author

greenais commented Dec 8, 2017

The swagger client is generated from the REST API docs. So it's exactly the same but presented differently

I'd like it to be so, but it unfortunately isn't. Even on the main page the last (.help() for final endpoint) screenshot is with copypaste mess.

In example, call to manager.query.put_db_design_ddoc.help() brings this:

* body (<span class="strong">View {</span><div><span class="propName ">_rev</span> (<span class="propType">string</span>, <span class="propOptKey">optional</span>): <span class="propDesc">Revision identifier of the parent revision the new one should replace. (Not used when creating a new document.)</span>,</div><div><span class="propName ">views</span> (<span class="propType">inline_model_2</span>, <span class="propOptKey">optional</span>): <span class="propDesc">List of views to save on this design document.</span></div><span class="strong">}</span><br /><span class="strong">inline_model_2 {</span><div><span class="propName ">my_view_name</span> (<span class="propType">inline_model_5</span>, <span class="propOptKey">optional</span>): <span class="propDesc">The view's map/reduce functions.</span></div><span class="strong">}</span><br /><span class="strong">inline_model_5 {</span><div><span class="propName ">map</span> (<span class="propType">string</span>, <span class="propOptKey">optional</span>): <span class="propDesc">Inline JavaScript definition for the map function</span>,</div><div><span class="propName ">reduce</span> (<span class="propType">string</span>, <span class="propOptKey">optional</span>): <span class="propDesc">Inline JavaScript definition for the reduce function</span></div><span class="strong">}</span>): The request body

And even if you will succeed to tear through this poorly formatted html copypaste - there in REST API are no such things like inline_model_2 or inline_model_5, any ideas what they are for?

In fact, I really appreciate contributors work on this plugin - as long as Couchbase itself still doesn't support React Native (I asked on CB forum - it's management decision, hardly reasonable to my point).
You are dong a great job in favor of Couchbase, guys, thank you.
I just merely want to help to make RNCBL a little bit more dev-friendly for those who will come later.

@jamesnocentini
Copy link
Contributor

jamesnocentini commented Dec 8, 2017

Even on the main page the last (.help() for final endpoint) screenshot is with copypaste mess.

It's how the swagger client works and we can't do anything about it. That HTML is the same as what's under the Model tab on swagger ui

We're using swagger-js 2.x https://github.com/couchbaselabs/react-native-couchbase-lite/blob/master/package.json#L34. We ought to try out swagger 3.x (as it appears to be out https://github.com/swagger-api/swagger-js) and might fix this issue.

@djpongh djpongh added the icebox label Dec 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants