groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Stagner <restag...@gmail.com>
Subject Re: Using RestClient to call a REST api and write the response to a file
Date Mon, 15 Jun 2015 21:16:34 GMT
Kurt,

By the looks of things, it appears you might be using HTTPBuilder.  If that
is the case, I've had a similar experience when I first started to use this
framework.

It's worth mentioning that there is another groovy-based web service
framework known as groovy-wslite
<https://github.com/jwagenleitner/groovy-wslite>.  When using this
framework, if you wanted to extract well-formed JSON from your response you
would simply use

def client = new RESTClient(baseUrl)
response = client.get(path: "/$context/movie",
        query: [movieId: 'toy-story'])

println response.contentAsString


On Sat, Jun 13, 2015 at 3:19 PM Kurt Andrews <kurt.w.andrews@gmail.com>
wrote:

> I'm trying to do something I though was very simple, but now appears more
> complicated.  I wanted to make an api call and store the JSON response in a
> file.  here is the code:
>
> def onSiteAg = new RESTClient("$endpoint".toString())
> def response = onSiteAg.get(
>   requestContentType: JSON,
>   path: "$path",
>   headers: ["X-OS-APIKey": "$apiKey".toString(),
>             "X-OS-Hash": "qxZira2KCZjF0j62hkvYmtvFFkw=",
>             "X-OS-userKey": "$userKey".toString(),
>             "X-OS-TimeStamp": "$ts".toString()])
>
> println response.data.toString()
>
> The response I get back isn't what I expected:
>
> [[children:[[children:[[children:[[children:[[children:[[created:2015-05-07
> 17:05:31.012675, field_id:100286, modified:2015-05-07 17:05:31.012675,
> name:Yorkville Mothers East, type:Field], [created:2015-05-07
> 17:05:31.125785, field_id:100287, modified:2015-05-07 17:05:31.125785,
> name:Yorkville NW, type:Field], [created:2015-05-07 17:05:31.187275,
> field_id:100288, modified:2015-05-07 17:05:31.187275, name:Yorkville
> Canton, type:Field], [created:2015-05-07 17:05:31.247555, field_id:100289,
> modified:2015-05-07 17:05:31.247555, name:Yorkville SE, type:Field],
> [created:2015-05-07 17:05:31.306773, field_id:100290, modified:2015-05-07
> 17:05:31.306773, name:Yorkville Rod, type:Field]], created:2015-05-07
> 17:05:30.995382, farm_id:32248, modified:2015-05-07 17:05:30.995382,
> name:Yorkville, type:Farm]], created:2015-05-07 17:05:30.98609,
> modified:2015-05-07 17:05:30.98609, name:N C IL, profile_id:12023,
> type:Customer]], created:2015-05-07 17:05:30.981996, modified:2015-05-07
> 17:05:30.981996, name:Growmark Location, profile_id:12022, type:Location]],
> created:2015-05-07 17:05:30.976894, modified:2015-05-07 17:05:30.976894,
> name:API Member Company, profile_id:12021, type:Dealer]],
> created:2015-05-05 19:31:41.792483, modified:2015-05-05 19:31:41.792483,
> name:FS AIS, profile_id:1, type:Site Owner]]
>
> I was expecting this:
>
> [{"type": "Site Owner", "name": "FS AIS", "profile_id": "1", "created": "2015-05-05 19:31:41.792483",
"modified": "2015-05-05 19:31:41.792483", "children":[{"type": "Dealer", "name": "API Member
Company", "profile_id": "12021", "created": "2015-05-07 17:05:30.976894", "modified": "2015-05-07
17:05:30.976894", "children":[{"type": "Location", "name": "Growmark Location", "profile_id":
"12022", "created": "2015-05-07 17:05:30.981996", "modified": "2015-05-07 17:05:30.981996",
"children":[{"type": "Customer", "name": "N C IL", "profile_id": "12023", "created": "2015-05-07
17:05:30.98609", "modified": "2015-05-07 17:05:30.98609", "children":[{"type": "Farm", "name":
"Yorkville", "farm_id": "32248", "created": "2015-05-07 17:05:30.995382", "modified": "2015-05-07
17:05:30.995382", "children":[{"type": "Field", "name": "Yorkville Mothers East", "field_id":
"100286", "created": "2015-05-07 17:05:31.012675", "modified": "2015-05-07 17:05:31.012675"
}, { "type": "Field", "name": "Yorkville NW", "field_id": "100287", "created": "2015-05-07
17:05:31.125785", "modified": "2015-05-07 17:05:31.125785" }, { "type": "Field", "name": "Yorkville
Canton", "field_id": "100288", "created": "2015-05-07 17:05:31.187275", "modified": "2015-05-07
17:05:31.187275" }, { "type": "Field", "name": "Yorkville SE", "field_id": "100289", "created":
"2015-05-07 17:05:31.247555", "modified": "2015-05-07 17:05:31.247555" }, { "type": "Field",
"name": "Yorkville Rod", "field_id": "100290", "created": "2015-05-07 17:05:31.306773", "modified":
"2015-05-07 17:05:31.306773"}]}]}]}]}]}]
>
> What I received seems reversed some how, and it looks like all of the JSON object syntax
has been removed.
>
> Can someone please explain what's happening, and how I can fix it so that I can dump
the JSON response from the server to a file?
>
> Thanks and regards,
>
> Kurt
>
>
>

Mime
View raw message