Hello,
I’m new to using Groovy and am looking to do a HTTP Request using the REST client to post data, unfortunately I cannot currently get it to work. I’ve added some logging as described on the REST client page (see below) but still cannot work out how to resolve the problem.
A little background – I’m trying to create a simple application that submits a post request, using rest into the API for tnspayments (a company who handle credit card payments), the code I am using is as follows (I recognise I’m not doing anything with the response).
String sendTransaction(String data, Merchant merchant) throws Exception {
Logger logger = Logger.getLogger(Transaction.class)
PropertyConfigurator.configure("src/log4j.xml")
RESTClient http = new RESTClient(merchant.gatewayHost)
http.auth.basic('', merchant.password)
try {
path:merchant.gatewayUrl,
body:data,
requestContentType: JSON) {
resp -> println "working"
}
}
catch(HttpResponseException ex) {
println ex.response.data
}
}
I know that all of my parameters (merchant.gatewayHost, merchant.password, merchant.gatewayUrl and data) are as I would expect them to be (I’m rewriting in to groovy from a rather ugly, but working example written in JSPs)
Log4J is giving me the following (with some of the data changed to X’s where necessary) – hence the error in the title, can anyone tell me how I’d resolve this error? I don’t seem to be able to specify the Character encoding
2011-10-31 13:35:10,569 DEBUG RESTClient - POST https://secure.ap.tnspayments.com/api/rest/version/4/merchant/XXXXXXXXXXXXXXX/order/100000000000000000/transaction/1
2011-10-31 13:35:10,715 DEBUG SingleClientConnManager - Get connection for route HttpRoute[{s}->https://secure.ap.tnspayments.com]
2011-10-31 13:35:12,785 DEBUG RequestAddCookies - CookieSpec selected: best-match
2011-10-31 13:35:12,794 DEBUG DefaultHttpClient - Attempt 1 to execute request
2011-10-31 13:35:12,794 DEBUG DefaultClientConnection - Sending request: POST /api/rest/version/4/merchant/XXXXXXXXXXXXXXX/order/100000000000000000/transaction/1 HTTP/1.1
2011-10-31 13:35:12,794 DEBUG wire - >> "POST /api/rest/version/4/merchant/XXXXXXXXXXXXXXX/order/100000000000000000/transaction/1 HTTP/1.1[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Accept: */*[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Content-Length: 267[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Content-Type: application/json[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Host: secure.ap.tnspayments.com[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Connection: Keep-Alive[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "Accept-Encoding: gzip,deflate[EOL]"
2011-10-31 13:35:12,795 DEBUG wire - >> "[EOL]"
2011-10-31 13:35:12,795 DEBUG headers - >> POST /api/rest/version/4/merchant/XXXXXXXXXXXXXXX/order/100000000000000000/transaction/1 HTTP/1.1
2011-10-31 13:35:12,795 DEBUG headers - >> Accept: */*
2011-10-31 13:35:12,795 DEBUG headers - >> Content-Length: 267
2011-10-31 13:35:12,795 DEBUG headers - >> Content-Type: application/json
2011-10-31 13:35:12,795 DEBUG headers - >> Host: secure.ap.tnspayments.com
2011-10-31 13:35:12,795 DEBUG headers - >> Connection: Keep-Alive
2011-10-31 13:35:12,795 DEBUG headers - >> Accept-Encoding: gzip,deflate
2011-10-31 13:35:12,797 DEBUG wire - >> "{"apiOperation":"PAY","cardDetails":{"card":{"number":"4508xxxxxx741019","expiry":{"month":"05", "year":"13"},"securityCode":"xxx"}},"order":{"reference":"none"},"transaction":{"amount":"250","currency":"GBP","reference":"1"},"customer":{"ipAddress":"xxx.xx.xx.xxx"}}"
2011-10-31 13:35:13,223 DEBUG wire - << "HTTP/1.1 400 Bad Request[EOL]"
2011-10-31 13:35:13,225 DEBUG wire - << "Date: Mon, 31 Oct 2011 13:37:21 GMT[EOL]"
2011-10-31 13:35:13,225 DEBUG wire - << "Pragma: no-cache[EOL]"
2011-10-31 13:35:13,225 DEBUG wire - << "Cache-Control: no-cache, no-store, must-revalidate[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "Expires: 0[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "Content-Type: application/json[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "Content-Length: 112[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "Connection: close[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "Set-Cookie: TSf8e18c=428e685961c7a85b061a762c847e9cf23879d840d037f73b4eaea492; Path=/[EOL]"
2011-10-31 13:35:13,226 DEBUG wire - << "[EOL]"
2011-10-31 13:35:13,226 DEBUG DefaultClientConnection - Receiving response: HTTP/1.1 400 Bad Request
2011-10-31 13:35:13,226 DEBUG headers - << HTTP/1.1 400 Bad Request
2011-10-31 13:35:13,226 DEBUG headers - << Date: Mon, 31 Oct 2011 13:37:21 GMT
2011-10-31 13:35:13,226 DEBUG headers - << Pragma: no-cache
2011-10-31 13:35:13,226 DEBUG headers - << Cache-Control: no-cache, no-store, must-revalidate
2011-10-31 13:35:13,226 DEBUG headers - << Expires: 0
2011-10-31 13:35:13,226 DEBUG headers - << Content-Type: application/json
2011-10-31 13:35:13,226 DEBUG headers - << Content-Length: 112
2011-10-31 13:35:13,226 DEBUG headers - << Connection: close
2011-10-31 13:35:13,226 DEBUG headers - << Set-Cookie: TSf8e18c=428e685961c7a85b061a762c847e9cf23879d840d037f73b4eaea492; Path=/
2011-10-31 13:35:13,231 DEBUG ResponseProcessCookies - Cookie accepted: "[version: 0][name: TSf8e18c][value: 428e685961c7a85b061a762c847e9cf23879d840d037f73b4eaea492][domain: secure.ap.tnspayments.com][path: /][expiry: null]".
2011-10-31 13:35:13,233 DEBUG RESTClient - Response code: 400; found handler: org.codehaus.groovy.runtime.MethodClosure@3488b1e6
2011-10-31 13:35:13,233 DEBUG RESTClient - Parsing response as: application/json
2011-10-31 13:35:13,260 WARN ParserRegistry - Could not find charset in response
2011-10-31 13:35:13,260 DEBUG wire - << "{"error":{"cause":"INVALID_REQUEST","explanation":"Character encoding has not been specified"},"result":"ERROR"}"
2011-10-31 13:35:13,260 DEBUG SingleClientConnManager - Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@7fb6634c
2011-10-31 13:35:13,260 DEBUG SingleClientConnManager - Released connection open but not reusable.
2011-10-31 13:35:13,261 DEBUG DefaultClientConnection - Connection shut down
2011-10-31 13:35:13,284 DEBUG RESTClient - Parsed data to instance of: class net.sf.json.JSONObject
Kind Regards,
Calvin Thomas
headers:['Content-Type':'application/json; charset=utf-8']
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Thank you very much! That's got me past that error - I couldn't for the life of me find how to actually specify that and that has removed the error. I've got another error now of "Request does not match any supported operation", but I suspect that is most likely a mistake in my JSON request string rather than a problem with my Groovy RESTClient code.
Many thanks again for your help.
Kind Regards,
Calvin Thomas
IT Manager
Wessex Group
Jewry House, 13 Jewry Street, Winchester, SO23 8RZ
Phone: 01962 892 146
Email: cth...@wessex-group.co.uk
Web: www.wessex-group.co.uk
-----Original Message-----
From: tmni...@gmail.com [mailto:tmni...@gmail.com]
Sent: 31 October 2011 14:31
To: us...@groovy.codehaus.org
Subject: Re: [groovy-user] RESTClient - HTTP 400 Bad Request Error - "{"error":{"cause":"INVALID_REQUEST","explanation":"Character encoding has not been specified"},"result":"ERROR"}"
The response says it's missing a character encoding. Try adding this arg to the post() parameters:
headers:['Content-Type':'application/json; charset=utf-8']
On Mon, Oct 31, 2011 at 9:41 AM, Calvin Thomas <cth...@wessex-group.co.uk> wrote:
> Hello,
>
>
>
> I'm new to using Groovy and am looking to do a HTTP Request using the
> REST client to post data, unfortunately I cannot currently get it to
> work. I've added some logging as described on the REST client page
> (see below) but still cannot work out how to resolve the problem.
>
>
>
> A little background - I'm trying to create a simple application that
> submits a post request, using rest into the API for tnspayments (a
> company who handle credit card payments), the code I am using is as
> follows (I recognise I'm not doing anything with the response).
>
>
>
> String sendTransaction(String data, Merchant merchant) throws
> Exception {
>
>
>
> Logger logger = Logger.getLogger(Transaction.class)
>
> PropertyConfigurator.configure("src/log4j.xml")
>
>
>
> RESTClient http = new RESTClient(merchant.gatewayHost)
>
> http.auth.basic('', merchant.password)
>
>
>
> try {
>
> http.post(
>
> path:merchant.gatewayUrl,
>
> body:data,
>
> requestContentType: JSON) {
>
> resp -> println "working"
>
> }
>
> }
>
>
>
> catch(HttpResponseException ex) {
>
> println ex.response.data
>
> }
>
> }
>
>
>
> I know that all of my parameters (merchant.gatewayHost,
> merchant.password, merchant.gatewayUrl and data) are as I would expect
> them to be (I'm rewriting in to groovy from a rather ugly, but working
> example written in
> JSPs)
>
>
>
> Log4J is giving me the following (with some of the data changed to X's
> where
> necessary) - hence the error in the title, can anyone tell me how I'd
---------------------------------------------------------------------