|
Hi guys,
I've been working on accomplishing the relatively simple task of sending a XML string through a POST request but am running into a lot of roadblocks .. I tried def request = """<?xml version="1.0" encoding="UTF-8"?> <requestMobilePaymentProcessEntrypoints xmlns="http://apps.shaun.com/shaunpay" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://apps.shaun.com/shaunpay/shaunpay.xsd"> <customerKey>a1w2e3r4g5</customerKey> <schCode>USK</schCode> <maxNumber>1</maxNumber> <items currency="USD" /> </requestMobilePaymentProcessEntrypoints> """ def url = new URL("http://apps.shaun.com/shaunpay/actions/default?method=lookup") def conn = url.openConnection() conn.setRequestProperty("Content-Type", "text/xml") conn.requestMethod='POST' //conn.addRequestProperty("request",request) conn.doOutput = true conn.doInput = true; Writer wr = new OutputStreamWriter(conn.outputStream) wr.write(request) wr.flush() wr.close() conn.connect() println conn.responseCode println conn.responseMessage and am getting a 400 status code error, which I take it to mean malformed syntax. I need to send the XML string as a value pair - name = "request", and xml string. Tried doing that with conn.addRequestProperty but got errors. Any help would be greatly appreciated. Also, I tried using HttpBuilder but it complained about a missing HttpRequestBase class. Added the Httpclient jar file into my lib folder to no avail. Am frustrated as hell.. Shaun |
|
Looks to me like your doing it the hard way. Check out HTTP Builder and
the rest plugin which uses HTTPBuilder On Thu, Oct 22, 2009 at 5:51 PM, ShaunLim <[hidden email]> wrote:
|
|
In reply to this post by shaun lim jin
forgot the link
http://groovy.codehaus.org/modules/http-builder/ On Thu, Oct 22, 2009 at 5:51 PM, ShaunLim <[hidden email]> wrote:
|
|
In reply to this post by shaun lim jin
You probably need the Http-Core JAR for HTTPBuilder as well. Download
this package: http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/0.5.0-RC2/http-builder-0.5.0-RC2-all.zip and it will have all of the necessary dependencies. As for your URL example above, I don't think you want to "connect" after writing to the request stream. Can you verify what (if any) data is getting to the server? Is it being sent as a POST request? -Tom 2009/10/22 ShaunLim <[hidden email]>: > > Hi guys, > > I've been working on accomplishing the relatively simple task of sending a > XML string through a POST request but am running into a lot of roadblocks .. > > I tried > > def request = """<?xml version="1.0" encoding="UTF-8"?> > <requestMobilePaymentProcessEntrypoints > xmlns="http://apps.shaun.com/shaunpay" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="http://apps.shaun.com/shaunpay/shaunpay.xsd"> > <customerKey>a1w2e3r4g5</customerKey> > <schCode>USK</schCode> > <maxNumber>1</maxNumber> > <items currency="USD" /> > </requestMobilePaymentProcessEntrypoints> > """ > > def url = new > URL("http://apps.shaun.com/shaunpay/actions/default?method=lookup") > def conn = url.openConnection() > > > conn.setRequestProperty("Content-Type", "text/xml") > conn.requestMethod='POST' > //conn.addRequestProperty("request",request) > conn.doOutput = true > conn.doInput = true; > > Writer wr = new OutputStreamWriter(conn.outputStream) > wr.write(request) > wr.flush() > wr.close() > > conn.connect() > println conn.responseCode > println conn.responseMessage > > and am getting a 400 status code error, which I take it to mean malformed > syntax. > > I need to send the XML string as a value pair - name = "request", and xml > string. Tried doing that with conn.addRequestProperty but got errors. > > Any help would be greatly appreciated. > > Also, I tried using HttpBuilder but it complained about a missing > HttpRequestBase class. Added the Httpclient jar file into my lib folder to > no avail. > > Am frustrated as hell.. > > Shaun > -- > View this message in context: http://www.nabble.com/Sending-XML-String-through-HTTP-post-tp26017830p26017830.html > Sent from the grails - user mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Hey Tom,
I got it to work .. somewhat. Here's how: def http = new HTTPBuilder('http://apps.shaun.com/shaunpay/actions/default?method=lookup') def postBody = [request:request] //parameter name: "request", value is a variable named "request" //def postHeaders = [Accept: 'application/xml'] //requestContentType: URLENC http.post( headers: [Accept: 'application/xml'], body: postBody ) { resp,reader -> println "-----Response-----" println "${resp.statusLine}" //def xml = new XmlSlurper().parse(reader) println reader.getClass().toString() println "\n------------------" } - The problem is that I'm expecting a XML return but am getting a groovy.util.slurpersupport.NodeChild return that I can't parse with XMLSlurper. Is there a way to explicitly get a XML return? Thanks! Shaun
|
|
The NodeChild is the parsed output from XmlSlurper :) HttpBuilder
does that part for you! Your response closure parameter named "reader" is actually a GPathResult, so you can do "println reader.name()" and "reader.children().each { println it.name() }" etc etc. 2009/10/23 ShaunLim <[hidden email]>: > > Hey Tom, > > I got it to work .. somewhat. Here's how: > > def http = new > HTTPBuilder('http://apps.shaun.com/shaunpay/actions/default?method=lookup') > def postBody = [request:request] //parameter name: "request", value > is a variable named "request" > //def postHeaders = [Accept: 'application/xml'] > //requestContentType: URLENC > http.post( headers: [Accept: 'application/xml'], body: postBody ) { > resp,reader -> > > println "-----Response-----" > println "${resp.statusLine}" > //def xml = new XmlSlurper().parse(reader) > println reader.getClass().toString() > println "\n------------------" > } > > - > > The problem is that I'm expecting a XML return but am getting a > groovy.util.slurpersupport.NodeChild return that I can't parse with > XMLSlurper. Is there a way to explicitly get a XML return? > > Thanks! > > Shaun > > > Tom Nichols wrote: >> >> You probably need the Http-Core JAR for HTTPBuilder as well. Download >> this package: >> http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/0.5.0-RC2/http-builder-0.5.0-RC2-all.zip >> and it will have all of the necessary dependencies. >> >> As for your URL example above, I don't think you want to "connect" >> after writing to the request stream. Can you verify what (if any) >> data is getting to the server? Is it being sent as a POST request? >> >> -Tom >> >> >> 2009/10/22 ShaunLim <[hidden email]>: >>> >>> Hi guys, >>> >>> I've been working on accomplishing the relatively simple task of sending >>> a >>> XML string through a POST request but am running into a lot of roadblocks >>> .. >>> >>> I tried >>> >>> def request = """<?xml version="1.0" encoding="UTF-8"?> >>> <requestMobilePaymentProcessEntrypoints >>> xmlns="http://apps.shaun.com/shaunpay" >>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >>> xsi:schemaLocation="http://apps.shaun.com/shaunpay/shaunpay.xsd"> >>> <customerKey>a1w2e3r4g5</customerKey> >>> <schCode>USK</schCode> >>> <maxNumber>1</maxNumber> >>> <items currency="USD" /> >>> </requestMobilePaymentProcessEntrypoints> >>> """ >>> >>> def url = new >>> URL("http://apps.shaun.com/shaunpay/actions/default?method=lookup") >>> def conn = url.openConnection() >>> >>> >>> conn.setRequestProperty("Content-Type", "text/xml") >>> conn.requestMethod='POST' >>> //conn.addRequestProperty("request",request) >>> conn.doOutput = true >>> conn.doInput = true; >>> >>> Writer wr = new OutputStreamWriter(conn.outputStream) >>> wr.write(request) >>> wr.flush() >>> wr.close() >>> >>> conn.connect() >>> println conn.responseCode >>> println conn.responseMessage >>> >>> and am getting a 400 status code error, which I take it to mean malformed >>> syntax. >>> >>> I need to send the XML string as a value pair - name = "request", and xml >>> string. Tried doing that with conn.addRequestProperty but got errors. >>> >>> Any help would be greatly appreciated. >>> >>> Also, I tried using HttpBuilder but it complained about a missing >>> HttpRequestBase class. Added the Httpclient jar file into my lib folder >>> to >>> no avail. >>> >>> Am frustrated as hell.. >>> >>> Shaun >>> -- >>> View this message in context: >>> http://www.nabble.com/Sending-XML-String-through-HTTP-post-tp26017830p26017830.html >>> Sent from the grails - user mailing list archive at Nabble.com. >>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe from this list, please visit: >>> >>> http://xircles.codehaus.org/manage_email >>> >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> >> > > -- > View this message in context: http://www.nabble.com/Sending-XML-String-through-HTTP-post-tp26017830p26032782.html > Sent from the grails - user mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Hi Shaun,
Just to follow up in case you're still working on this -- I've updated the site documentation here: http://groovy.codehaus.org/modules/http-builder/doc/xml.html With a working example that should help you understand it better. Thanks. -Tom On Sat, Oct 24, 2009 at 4:56 PM, Tom Nichols <[hidden email]> wrote: > The NodeChild is the parsed output from XmlSlurper :) HttpBuilder > does that part for you! Your response closure parameter named > "reader" is actually a GPathResult, so you can do "println > reader.name()" and "reader.children().each { println it.name() }" etc > etc. > > 2009/10/23 ShaunLim <[hidden email]>: >> >> Hey Tom, >> >> I got it to work .. somewhat. Here's how: >> >> def http = new >> HTTPBuilder('http://apps.shaun.com/shaunpay/actions/default?method=lookup') >> def postBody = [request:request] //parameter name: "request", value >> is a variable named "request" >> //def postHeaders = [Accept: 'application/xml'] >> //requestContentType: URLENC >> http.post( headers: [Accept: 'application/xml'], body: postBody ) { >> resp,reader -> >> >> println "-----Response-----" >> println "${resp.statusLine}" >> //def xml = new XmlSlurper().parse(reader) >> println reader.getClass().toString() >> println "\n------------------" >> } >> >> - >> >> The problem is that I'm expecting a XML return but am getting a >> groovy.util.slurpersupport.NodeChild return that I can't parse with >> XMLSlurper. Is there a way to explicitly get a XML return? >> >> Thanks! >> >> Shaun >> >> >> Tom Nichols wrote: >>> >>> You probably need the Http-Core JAR for HTTPBuilder as well. Download >>> this package: >>> http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/0.5.0-RC2/http-builder-0.5.0-RC2-all.zip >>> and it will have all of the necessary dependencies. >>> >>> As for your URL example above, I don't think you want to "connect" >>> after writing to the request stream. Can you verify what (if any) >>> data is getting to the server? Is it being sent as a POST request? >>> >>> -Tom >>> >>> >>> 2009/10/22 ShaunLim <[hidden email]>: >>>> >>>> Hi guys, >>>> >>>> I've been working on accomplishing the relatively simple task of sending >>>> a >>>> XML string through a POST request but am running into a lot of roadblocks >>>> .. >>>> >>>> I tried >>>> >>>> def request = """<?xml version="1.0" encoding="UTF-8"?> >>>> <requestMobilePaymentProcessEntrypoints >>>> xmlns="http://apps.shaun.com/shaunpay" >>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >>>> xsi:schemaLocation="http://apps.shaun.com/shaunpay/shaunpay.xsd"> >>>> <customerKey>a1w2e3r4g5</customerKey> >>>> <schCode>USK</schCode> >>>> <maxNumber>1</maxNumber> >>>> <items currency="USD" /> >>>> </requestMobilePaymentProcessEntrypoints> >>>> """ >>>> >>>> def url = new >>>> URL("http://apps.shaun.com/shaunpay/actions/default?method=lookup") >>>> def conn = url.openConnection() >>>> >>>> >>>> conn.setRequestProperty("Content-Type", "text/xml") >>>> conn.requestMethod='POST' >>>> //conn.addRequestProperty("request",request) >>>> conn.doOutput = true >>>> conn.doInput = true; >>>> >>>> Writer wr = new OutputStreamWriter(conn.outputStream) >>>> wr.write(request) >>>> wr.flush() >>>> wr.close() >>>> >>>> conn.connect() >>>> println conn.responseCode >>>> println conn.responseMessage >>>> >>>> and am getting a 400 status code error, which I take it to mean malformed >>>> syntax. >>>> >>>> I need to send the XML string as a value pair - name = "request", and xml >>>> string. Tried doing that with conn.addRequestProperty but got errors. >>>> >>>> Any help would be greatly appreciated. >>>> >>>> Also, I tried using HttpBuilder but it complained about a missing >>>> HttpRequestBase class. Added the Httpclient jar file into my lib folder >>>> to >>>> no avail. >>>> >>>> Am frustrated as hell.. >>>> >>>> Shaun >>>> -- >>>> View this message in context: >>>> http://www.nabble.com/Sending-XML-String-through-HTTP-post-tp26017830p26017830.html >>>> Sent from the grails - user mailing list archive at Nabble.com. >>>> >>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe from this list, please visit: >>>> >>>> http://xircles.codehaus.org/manage_email >>>> >>>> >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe from this list, please visit: >>> >>> http://xircles.codehaus.org/manage_email >>> >>> >>> >>> >> >> -- >> View this message in context: http://www.nabble.com/Sending-XML-String-through-HTTP-post-tp26017830p26032782.html >> Sent from the grails - user mailing list archive at Nabble.com. >> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | Edit this page |
