Quick question: HTTP with REST download from the server?

I would like to know how download data from REST over HTTP protocol with the help of web service with method “POST” and Content-Type: application/xml? Thanks

https://github.com/JuliaWeb/Requests.jl/ is exactly what you are after.

This package does not support XML, but JSON. If I did mistake please tell me.

When I use the Requests.jl package, I will use the JSON package to parse:

Resp = Requests.get(url) |> Requests.readall

data = JSON.parse(Resp)

I believe you could do something similar with an XML package like LightXML.jl, though I have not done so.

EDIT: added Requests.readall to code, changed Response to Requests

Here is some example code that might be useful. As @pyrex41 said, Requests will give you the xml string, which you can use a regular XML parser to process.

using Requests
using LightXML

r=Requests.get("https://gist.githubusercontent.com/aviks/e71eb08713273329bd940c9cc07c0973/raw/d0e0dcb07d6ae00ad233e48e0a07011fac2e5873/note.xml")
n=LightXML.parse_string(Requests.readall(r))

n now contains a parsed xml document, which you can then use the appropriate DOM methods. For example

julia> get_elements_by_tagname(root(n), "from")
1-element Array{LightXML.XMLElement,1}:
 <from>Jani</from>

Hope this helps. (Note I’ve used module prefixed methods here for clarity, most of these functions are exported).

Regards

Avik

1 Like

Thanks @avik and @pyrex41 for the introduction. This helped me a lot. Now I must first send the following XML(XSD) request for BODY e.g.:


<ws:dataDeliveryRequest dateFrom="2017-01-15" dateTo="2017-01-15"
        xmlns="http://geomodel.eu/schema/data/request"
        xmlns:ws="http://geomodel.eu/schema/ws/data"
        xmlns:pv="http://geomodel.eu/schema/common/pv"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         
    <site id="site1" lat="48.61259" lng="20.827079">
        <pv:system installedPower="5.6">
            <pv:module type="ASI"/>
            <pv:inverter/>
            <pv:losses/>
        </pv:system>
    </site>
    <processing key="GHI DIF " summarization="MIN_15"/>
     
</ws:dataDeliveryRequest>

HEADER I setup on Dict(“Content-Type” => “text/xml”). My request is

xdoc = LightXML.parse_file("ex1.xml")
out = Requests.post("https://solargis.info/ws/rest/datadelivery/request?key=demo"; headers = Dict("Content-Type" => "text/xml"), data = Dict("body" => xdoc)) |> Requests.readall

but returns me error. Response from the server

HTTP Status 500 - Request processing failed; nested exception is java.lang.RuntimeException: Could not validate source: Content is not allowed in prolog.; nested exception is org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

Some idea how to implement a XML file?

So the web service you are calling expects the xml to be directly in the request body. Passing a Dict to Requests sends the data as “html forms”. However, in this case, the xml should be directly present, not encoded as a form input. So this works:

julia>  out = Requests.post("https://solargis.info/ws/rest/datadelivery/request?key=demo"; headers = Dict("Content-Type" => "text/xml"), data = string(xdoc)) |> Requests.readall

"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><dataDeliveryResponse xmlns=\"http://geomodel.eu/schema/ws/data\" 
xmlns:ns2=\"http://geomodel.eu/schema/common/geo\"><site id=\"site1\" lat=\"48.61259\" lng=\"20.827079\"><metadata>#15 MINUTE VALUES OF SOLAR
RADIATION&#xD;\n#&#xD;\n#Issued: 2017-01-27 09:40&#xD;\n#&#xD;\n#Latitude: 48.612590&#xD;\n#Longitude:     20.827079&#xD;\n#http://solargis.info/imaps/
#tl=Google:satellite&amp;loc=48.612590,20.827079&amp;z=14 &#xD;\n#&#xD;\n#&#xD;\n#Output from the climate database Solargis 
v2.1.8&#xD;\n#&#xD;\n#Solar radiation data&#xD;\n#Description: data calculated from Meteosat MSG and MFG satellite data ((c) 2017 EUMETSAT) 
and from atmospheric data ((c) 2017 ECMWF and NOAA) by Solargis method &#xD;\n#Summarization type: instantaneous&#xD;\n#Summarization period: 15/01/2017 - 15/01/2017&#
........

Also, you do not need to parse the xml if you are reading it from the disk, since it gets send as a string over http in any case. But if you are creating the xdoc dynamically, then, as I’ve done above, convert it back to string before passing it to Requests.post.

2 Likes