You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Kurt Andrews <ku...@gmail.com> on 2015/06/14 00:18:23 UTC
Using RestClient to call a REST api and write the response to a file
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
Re: Using RestClient to call a REST api and write the response to a file
Posted by Dinko Srkoč <di...@gmail.com>.
`response.data` is a data structure that represents the data from the
request. In your case it's a list of maps. You can get JSON string by
using e.g. `groovy.json.JsonOutput`:
println groovy.json.JsonOutput.toJson(response.data)
Cheers,
Dinko
On 14 June 2015 at 01:27, Kurt Andrews <ku...@gmail.com> wrote:
> Owen, thanks for the quick reply.
> You are correct, all of the data is there, but some transformation has taken
> place that I didn't expect, I didn't want to reference anything yet, just
> dump the response to a file. It looks as though the RestClient traversed the
> entire tree recursively to build a map of maps to convert the response to a
> string. I need the response in a Jason file to be processed down stream.
>
> On Sat, Jun 13, 2015 at 5:34 PM Owen Rubel <or...@gmail.com> wrote:
>>
>> Looks like you merely lost the ROOT 'type' in your referencing; everything
>> else is there. This shouldn't be a problem. Just make sure you are
>> referencing it properly.
>>
>> Owen Rubel
>> 415-971-0976
>> orubel@gmail.com
>>
>> On Sat, Jun 13, 2015 at 3:18 PM, Kurt Andrews <ku...@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
>>>
>>>
>>>
>>>
>>
>
Re: Using RestClient to call a REST api and write the response to a file
Posted by Kurt Andrews <ku...@gmail.com>.
Owen, thanks for the quick reply.
You are correct, all of the data is there, but some transformation has
taken place that I didn't expect, I didn't want to reference anything yet,
just dump the response to a file. It looks as though the RestClient
traversed the entire tree recursively to build a map of maps to convert the
response to a string. I need the response in a Jason file to be processed
down stream.
On Sat, Jun 13, 2015 at 5:34 PM Owen Rubel <or...@gmail.com> wrote:
> Looks like you merely lost the ROOT 'type' in your referencing; everything
> else is there. This shouldn't be a problem. Just make sure you are
> referencing it properly.
>
> Owen Rubel
> 415-971-0976
> orubel@gmail.com
>
> On Sat, Jun 13, 2015 at 3:18 PM, Kurt Andrews <ku...@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
>>
>>
>>
>
Re: Using RestClient to call a REST api and write the response to a file
Posted by Owen Rubel <or...@gmail.com>.
Looks like you merely lost the ROOT 'type' in your referencing; everything
else is there. This shouldn't be a problem. Just make sure you are
referencing it properly.
Owen Rubel
415-971-0976
orubel@gmail.com
On Sat, Jun 13, 2015 at 3:18 PM, Kurt Andrews <ku...@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
>
>
>
Re: Using RestClient to call a REST api and write the response to a file
Posted by Kurt Andrews <ku...@gmail.com>.
Robert,
Thanks for the heads up on groovy-wslite. It looks like it may do the
trick.
On Mon, Jun 15, 2015 at 4:16 PM Robert Stagner <re...@gmail.com> wrote:
> 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 <ku...@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
>>
>>
>>
Re: Using RestClient to call a REST api and write the response to a file
Posted by Guillaume Laforge <gl...@gmail.com>.
...and response.json gives a parsed JSON structure (ie. maps of lists or
lists of maps)
Guillaume
2015-06-15 23:16 GMT+02:00 Robert Stagner <re...@gmail.com>:
> 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 <ku...@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
>>
>>
>>
--
Guillaume Laforge
Groovy Project Manager
Product Ninja & Advocate at Restlet <http://restlet.com>
Blog: http://glaforge.appspot.com/
Social: @glaforge <http://twitter.com/glaforge> / Google+
<https://plus.google.com/u/0/114130972232398734985/posts>
Re: Using RestClient to call a REST api and write the response to a file
Posted by Robert Stagner <re...@gmail.com>.
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 <ku...@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
>
>
>