# Quick question: HTTP with REST download from the server?

**URL:** <https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541>\
**Category:** General Usage\
**Created:** [January 17, 2017, 11:01am UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541 "2017-01-17T11:01:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [January 17, 2017, 11:01am UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/1 "2017-01-17T11:01:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [January 17, 2017, 12:04pm UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/2 "2017-01-17T12:04:02Z")

</div>

[https://github.com/JuliaWeb/Requests.jl/](https://github.com/JuliaWeb/Requests.jl/) is exactly what you are after.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [January 17, 2017, 12:28pm UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/3 "2017-01-17T12:28:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [January 17, 2017, 12:58pm UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/4 "2017-01-17T12:58:05Z")

</div>

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

```julia
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

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [January 17, 2017, 1:17pm UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/5 "2017-01-17T13:17:04Z")

</div>

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.

```julia
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-auto
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

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [January 27, 2017, 8:17am UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/6 "2017-01-27T08:17:32Z")

</div>

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.:

```julia

<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

```julia
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?

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [January 27, 2017, 8:45am UTC](https://discourse.julialang.org/t/quick-question-http-with-rest-download-from-the-server/1541/7 "2017-01-27T08:45:31Z")

</div>

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.
