xlordt
xlordt

Reputation: 521

httpBuilder - passing GetWeather as parameter

new to Groovy so please forgive for the lack of knowledge (been learning groovy for two weeks now). Anyways, I am trying to send a soap request to globalweather.asmx?WSDL while passing GetWeather as a parameter instead of passing XML text, however I can't seem to get the right result. I tried searching for a tutorial on how to achieve this but failed, most of the tutorials are passing the actual XML. Below is what I have tried so far.

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method


def http     = new HTTPBuilder('http://www.webservicex.com/globalweather.asmx?WSDL')
def postBody = ['Newark', 'USA'];
//body = postBody
println http.get(path: '', query: [GetWeather: postBody])

Upvotes: 0

Views: 884

Answers (1)

sensei
sensei

Reputation: 621

We'd need a lot more information to really help you: are you trying to request the WS as a SOAP or REST WebService ? If the latter, GET or POST ?

Since you're using the get method of HTTPBuilder, I'll assume you're going for the REST GET version. In which case, what you're trying to to is the programmatic equivalent of pointing your browser to the http://www.webservicex.com/globalweather.asmx/GetWeather?CityName=Newark&CountryName=USA URL.

Let's first analyse what this means. When you click that link, your browser will send a HTTP GET request to the www.webservicex.com server. That request is nothing special, the only real interesting bit being the URL itself. However, the server will decompose the URL in 3 parts (schematized for simplicity's sake):

  • The host: www.webservicex.com
  • The path: /globalweather.asmx/GetWeather
  • The query parameters: [CityName: 'Newark', CountryName: 'USA']

As you can see, GetWeather is not a parameter but a part of the path itself. Further, since it looks confusing, I'd rename postBody, which looks like a variable for the body of a HTTP POST request, to query (or pass the data directly to the get method, in this case).

Which gives us the following code (also removing unused imports):

import groovyx.net.http.HTTPBuilder

def http     = new HTTPBuilder('http://www.webservicex.com/globalweather.asmx?WSDL')
println http.get(path: '/globalweather.asmx/GetWeather', query: [CityName: 'Newark', CountryName: 'USA'])

Which works: the value returned (Data Not Found) being the same as when using the form on http://www.webservicex.com/New/Home/ServiceDetail/56 in a browser with the same input values.

Documentation used:

Upvotes: 1

Related Questions