How do I make a REST GET request (with authentication) and parse the result in javascript?
Due to circumstances beyond my control Javascript
- the only option for me. I am a beginner and not even sure if I am approaching the problem in the "recommended" way.
To put it simply, the client set up the server MarkLogicDB
online and gave me read-only access. I can request a server with an HTTP GET protocol to return a document XML
to be parsed. I managed to create a command curl
to return the data I needed (example below);
curl --anyauth --user USERNAME:PASSWORD \
-X GET \
http://test.com:8020/v1/documents?uri=/path/to/file.xml
The above returns the requested file XML
. Can someone please show me how I could convert the above code Javascript
? Also, how would I parse the data? Let's say I want to get all the information from a specific element or attribute. How can I do that?
It would be trivial for me to do it in Java/.NET
, but after reading a lot of online Javascript tutorials my head is spinning. Each tutorial talks about web browsers, but I am doing it in a server environment ( parse.com CloudCode ). There is no interface or HTML. For debugging purposes, I just read the logs generated with console.log()
.
source to share
https://parse.com/docs/cloud_code_guide#networking seems pretty clear as far as it gets.
Parse.Cloud.httpRequest({
url: 'http://test.com:8020/v1/documents',
params: {
uri : '/path/to/file.xml'
},
success: function(httpResponse) {
console.log(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
But you also need authentication. There are Parse.Cloud.httpRequest
no examples in the docs for this. If you have support with this provider, ask the provider about Digest Authentication .
If you get stuck, try adding user
and password
in options httpRequest
and see what happens. This can work if the developers of this stack followed the XMLHttpRequest convention.
In the absence of vendor support and existing functionality, you will have to authenticate yourself in JavaScript. This works by creating lines that go into the request headers. These resources should help:
- http://en.wikipedia.org/wiki/Digest_access_authentication
- http://en.wikipedia.org/wiki/Basic_access_authentication
Basic auth is much easier to implement, but I recommend using the digest for security reasons. If your HTTPServer doesn't support this, try changing your configuration.
source to share