Couchbase Lite _replicate 400 BAD REQUEST with Cordova (PhoneGap) and Ionic Framework (AngularJS) $http on iOS

261 views
Skip to first unread message

n8s...@gmail.com

unread,
Mar 12, 2015, 11:19:51 AM3/12/15
to mobile-c...@googlegroups.com
I cannot get Coucbase Lite _replicate to work with Cordova (PhoneGap), using the Ionic Framework (AngularJS) and javascript $http call. $http always results in a 400 BAD REQUEST from Couchbase Lite. I'm uncertain about the proper config settings, nor how to debug the error at a deeper level (yet).

Due to my existing codebase, I want to use $http.post with Couchbase Lite. My Couchbase server and Sync Gateway are working together fine and are configured to use bucket shadowing between the sync bucket and primary server bucket.

DETAILS:

### CBL ERROR REASON: {"data":{"status":400,"error":"bad_request"},"status":400,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"url":"http://lite.couchbase./_replicate","data":{"target":”app-cbl","source":"10.69.0.117/firefly_sg/","continuous":"true"},"contentType":"application/json","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Bad Request”}

//
// Cordova code running on iOS
//
this.startReplication = function () {
    var deferred = $q.defer();

    // -- I have tried multiple configs, using both $http and $http.post, and also revised source / target --
    // -- This config is the closest to working, but results in a 400 error. --
    var config = {
        method: 'POST',
        url: "http://lite.couchbase./_replicate",
        data: {
            target: ’app-cbl’,
            source: ’10.60.0.100/sync_gateway/',
            continuous: 'true'
        },
        contentType: 'application/json'
    }

    $http(config).then(function (result) {
        console.log('### CBL SUCCESS RESULT: ' + JSON.stringify(result));
        deferred.resolve(result);
    }, function (reason) {
        console.log('### CBL ERROR REASON: ' + JSON.stringify(reason));
        deferred.reject(reason);
    }).catch(function (err) {
        console.log('### CBL CATCH ERROR: ' + JSON.stringify(err));
        deferred.reject(err);
    })
    return deferred.promise;
}

I have also tried different URLs for the sync gateway, but still get the same 400 error so I assume the error is originating locally on the iOS side.

Thank you for any assistance.

Jens Alfke

unread,
Mar 12, 2015, 12:56:20 PM3/12/15
to mobile-c...@googlegroups.com

On Mar 12, 2015, at 8:09 AM, n8s...@gmail.com wrote:

### CBL ERROR REASON: {"data":{"status":400,"error":"bad_request"},"status":400,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"url":"http://lite.couchbase./_replicate","data":{"target":”app-cbl","source":"10.69.0.117/firefly_sg/","continuous":"true"},"contentType":"application/json","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Bad Request”}

The value of “continuous” should be true, not “true”. I.e. a boolean not a string.

—Jens

Jens Alfke

unread,
Mar 12, 2015, 12:58:00 PM3/12/15
to mobile-c...@googlegroups.com
Oh, also, the “source” string is not a valid URL — it’s missing the protocol.

—Jens

n8s...@gmail.com

unread,
Mar 13, 2015, 4:17:13 PM3/13/15
to mobile-c...@googlegroups.com
That was it -- fixed the 400 error -- Thank you!

I'm still not seeing a sync occur and not sure how to debug what's happening internally on Couchbase Lite from a Cordova / PhoneGap perspective.

Q1: Is there a way to get Couchbase Lite log / debug info from within a Cordova / Phonegap environment? Without it, I'm flying blind.

Q2: What is the correct way to include both Credentials and Channel information in the $http call? For example, should they be embedded in the url http://user:pass...@ipaddress.com or in the within the config object somehow? (Not Facebook Auth)

Jens Alfke

unread,
Mar 16, 2015, 12:15:46 PM3/16/15
to mobile-c...@googlegroups.com
On Mar 13, 2015, at 1:17 PM, n8s...@gmail.com wrote:

Q1: Is there a way to get Couchbase Lite log / debug info from within a Cordova / Phonegap environment? Without it, I'm flying blind.

Hm, I’m not an expert on this, JChris is. Can you configure the command-line arguments passed to the app on launch? If so, you can enable logging that way. (I’m assuming you’re using the native/Cocoa CBL, not the Xamarin/.NET one.)

Q2: What is the correct way to include both Credentials and Channel information in the $http call? For example, should they be embedded in the url http://user:pass...@ipaddress.com or in the within the config object somehow? (Not Facebook Auth)

Yes, put the username/password in the URL. (They are stripped out of the URL before any HTTP requests are sent, so they won’t show up in proxy logs or anything like that.)

—Jens
Reply all
Reply to author
Forward
0 new messages