dildik
dildik

Reputation: 405

Grails HttpBuilder URL encoded GET

I want to communicate with a webservice, which has a URL-based API. For example I have the following URL:

http://api.bla.com/aaa/bbb.ashx?Action=GetSecurityToken&vendorId=3

I can put the URL into the browser and get a XML page, with all details.

I want to get the XML page from my grails application, therefore i use the following code:

http = new HTTPBuilder('http://api.bla.com/aaa/bbb.ashx')
html = http.get( path : '/', query : [Action :"GetSecurityToken", vendorId: "3"] ) )
println html

Why doesnt this work. I get a bad request. How can I get the xml page from the URL above in my grails controller?

Upvotes: 1

Views: 1250

Answers (1)

user800014
user800014

Reputation:

I think that the final url will be http://api.bla.com/aaa/bbb.ashx/?Action=GetSecurityToken&vendorId=3 , because you defined your base url as http://api.bla.com/aaa/bbb.ashx and set the path of your call to /.

Try changing your base url like (taken from this example):

def http = new HTTPBuilder('http://api.bla.com/aaa')
http.get( path : '/bbb.ashx',
          contentType : XML,
          query : [Action :"GetSecurityToken", vendorId: "3"] ) { resp, reader ->

  println "response status: ${resp.statusLine}"
  println 'Headers: -----------'
  resp.headers.each { h ->
    println " ${h.name} : ${h.value}"
  }
  println 'Response data: -----'
  System.out << reader
  println '\n--------------------'
}

Upvotes: 1

Related Questions