Esau Romano
Posts: 0
Joined: Sun Jun 02, 2013 7:03 pm

How to get server code to post or put to couchdb?

self explanatory

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
var XHRResponse = XHR.send("PUT", "http://username:password@ipaddress/te...", {
headers:{
'Content-Type':'application/json',
'accept':'application/json'
},
data:{
"Subject":"I like Plankton",
"Author":"Rusty",
"PostedDate":"2006-08-15T17:30:12-04:00",
"Tags":["plankton", "baseball", "decisions"],
"Body":"I decided today that I don't like baseball. I like plankton."
}
});
responseBody.requestBody = request.body();
responseBody.requestParams = requestParams;
console.log(XHRResponse.body);
response.success(XHRResponse.body, "application/json");

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

How to get server code to post or put to couchdb?

Hi Esau,

Looks okay,

Do you have some problem with this request?

Regards.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to get server code to post or put to couchdb?

Hello Esau!

Just to add - here is how to send requests from servercode using XMLHttpRequest http://devcenter.appery.io/documentat.... Here is an API description http://docs.couchdb.org/en/latest/int...

Esau Romano
Posts: 0
Joined: Sun Jun 02, 2013 7:03 pm

How to get server code to post or put to couchdb?

Hello Yurii and Maryna

Your examples in the tutorial only includes, GET, while im in the urgency to use POST and PUT verbes, please elaborate on your answer and, if posible, please translate this curl commands to individual scripts i can use on the script platform.

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed... -d '{"title":"There is Nothing Left to Lose","artist":"Foo Fighters"}'

curl -vX PUT http://127.0.0.1:5984/albums/6e1295ed... \
--data-binary @artwork.jpg -H "Content-Type:image/jpg"

curl -vX POST http://127.0.0.1:5984/_replicate \
-d '{"source":"albums","target":"http://example.org:5984/albums-replica"}' \
-H "Content-Type:application/json"

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to get server code to post or put to couchdb?

Hello!

Here is an example how would your first request look like:

prevar XHRResponse = XHR&#46;send("GET", "http:&#47;&#47;127&#46;0&#46;0&#46;1:5984/albums/6e1295ed6c29495e54cc05947f18c8af", {"title":"There is Nothing Left to Lose","artist":"Foo Fighters"});
result = "status: " + XHRResponse&#46;status;
result += "body: " + XHRResponse&#46;body;
response&#46;success(result, "text/plain");/prePlease note 127.0.0.1 is a localhost, not real address. You need to replace it with real one.

Esau Romano
Posts: 0
Joined: Sun Jun 02, 2013 7:03 pm

How to get server code to post or put to couchdb?

THIS DOES NOT RUN AND IS NOT A POST, PLEASE BE SO GENTLE TO GIVE A CORRECT EXAMPLE

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
var XHRResponse = XHR.send("GET", "http://USER:PASSWORD@IPADDRESS/prueba...", {"title":"There is Nothing Left to Lose","artist":"Foo Fighters"});
result = "status: " + XHRResponse.status;
result += "body: " + XHRResponse.body;
response.success(result, "text/plain");

IT GIVES ME THIS

Script genericpost: Error: NullPointerException ( @ 8 : 45 ) - var innerResult = GlobalXHRInner.send(method, url, GlobalJSON.stri

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to get server code to post or put to couchdb?

Hi Esau,

This is an incorrect URL:
"http://USER:PASSWORD@ipaddress/prueba..."

Please use the following:
"http://example.com/param1/param2"

Esau Romano
Posts: 0
Joined: Sun Jun 02, 2013 7:03 pm

How to get server code to post or put to couchdb?

but, how do i read the params and send them as user and password?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

How to get server code to post or put to couchdb?

Hello Esau,

Sorry for misunderstanding. This is the normal URL. The third parameter of the send method is not set correctly.
PLease look here to see its structure: http://devcenter.appery.io/documentat...
For example, the right structure looks like:
prevar XHRResponse = XHR&#46;send("POST", "http:&#47;&#47;USER:PASSWORD@ipaddress/prueba/okok", {
"body": {
"source": "albums",
"target": "http:&#47;&#47;example&#46;org:5984/albums-replica"
},
"headers": {
"Content-Type": "application/json"
}
});/pre

Esau Romano
Posts: 0
Joined: Sun Jun 02, 2013 7:03 pm

How to get server code to post or put to couchdb?

Evgene, you are the man!, that was it!, now everything someone could need to control couchdb api from appery.io's server code is in this post, i can even write a tutorial if you like

Return to “Issues”