BASEX: REST: PUT to change a XML file in DB
Monday, 10 July 2017 · 3 messages
RK
Roland Krause10 Jul 2017 15:16 #
Hi all,
I installed basex for keeping and updating XML files.
I can load a XML into a browser with javascript
...
var xhr = new XMLHttpRequest();
xhr.open( "GET", "http://localhost:8984/rest/XMLfiles/cmuInput.xml", true); xhr.send();
var xml = xhr.responseXML;
...
but if I try to PUT something, it will not work.
...
var xhr = new XMLHttpRequest();
xhr.open( "PUT", "http://localhost:8984/rest/XMLfiles/test.xml", true); xhr.setRequestHeader("Authorization", "Basic " + btoa("admin:admin")); xhr.send("<ALLES>hat ein Ende, nur die Wurst hat zwei</ALLES>"); ...
*[HTTP/1.1 400 Bad Request 6ms]*
BUT it works with *curl*
echo "<ALLES>hat ein Ende, nur die Wurst hat zwei</ALLES>" | *curl* -u admin:admin -i -X PUT -T - "http://
localhost
:8984/rest/XMLfiles/
test
.xml"
HTTP/1.1 201 Created
Content-Type: text/plain;charset=UTF-8
Content-Length: 36
Server: Jetty(8.1.18.v20150929)
1 resource(s) replaced in 32.91 ms.
I need help here!
--
Kind regards,
*Roland*
CG
Christian Grün10 Jul 2017 15:51 #
Dear Roland,
This sounds like a JavaScript issue, so I can’t help too much, but maybe thing work out if you specify login and passwort directly in the URL?
xhr.open( "PUT",
"http://admin:admin@localhost:8984/rest/XMLfiles/test.xml", true);
Cheers,
Christian
Quoted text · Roland Krause · 44 lines
On Mon, Jul 10, 2017 at 3:16 PM, Roland Krause <roland.krause at globalfoundries.com> wrote: > Hi all, > > I installed basex for keeping and updating XML files. > > I can load a XML into a browser with javascript > ... > var xhr = new XMLHttpRequest(); > xhr.open( "GET", "http://localhost:8984/rest/XMLfiles/cmuInput.xml", true); > xhr.send(); > var xml = xhr.responseXML; > ... > > but if I try to PUT something, it will not work. > ... > var xhr = new XMLHttpRequest(); > xhr.open( "PUT", "http://localhost:8984/rest/XMLfiles/test.xml", true); > xhr.setRequestHeader("Authorization", "Basic " + btoa("admin:admin")); > xhr.send("<ALLES>hat ein Ende, nur die Wurst hat zwei</ALLES>"); > ... > > [HTTP/1.1 400 Bad Request 6ms] > > BUT it works with curl > > echo "<ALLES>hat ein Ende, nur die Wurst hat zwei</ALLES>" | curl -u > admin:admin -i -X PUT -T - "http:// > localhost > :8984/rest/XMLfiles/ > test > .xml" > > HTTP/1.1 201 Created > Content-Type: text/plain;charset=UTF-8 > Content-Length: 36 > Server: Jetty(8.1.18.v20150929) > 1 resource(s) replaced in 32.91 ms. > > I need help here! > > -- > Kind regards, > Roland
MH
Quoted text · Roland Krause · 22 lines
On 10.07.2017 15:16, Roland Krause wrote: > Hi all, > > I installed basex for keeping and updating XML files. > > I can load a XML into a browser with javascript > ... > var xhr = new XMLHttpRequest(); > xhr.open( "GET", "http://localhost:8984/rest/XMLfiles/cmuInput.xml", true); > xhr.send(); > var xml = xhr.responseXML; > ... > > but if I try to PUT something, it will not work. > ... > var xhr = new XMLHttpRequest(); > xhr.open( "PUT", "http://localhost:8984/rest/XMLfiles/test.xml", true); > xhr.setRequestHeader("Authorization", "Basic " + btoa("admin:admin")); > xhr.send("<ALLES>hat ein Ende, nur die Wurst hat zwei</ALLES>"); > ... > > *[HTTP/1.1 400 Bad Request 6ms]*
I have never tried a PUT with client-side Javascript but as far as I remember from POST requests if you want to send XML you should pass a DOM document to the send method e.g. in your case you can parse the string into a document using DOMParser
xhr.send(new DOMParser().parseFromString('<test/>', 'application/xml'));
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery