5

My request looks like this but when I try to run this code I get this error:

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

import groovy.json.JsonOutput

import groovy.json.JsonBuilder
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*
import groovyx.net.http.HttpResponseException


public PaymentSpring() throws Exception {
def username ='test_XXXXXXXXXXXXXXXXXXXX'
def resp
def https = new RESTClient('https://api.paymentspring.com/api/v1')
https.auth.basic(username,'')
def charge= [
    card_owner_name        : 'test tset',
    card_number      : '345829002709133',
    card_exp_month:'12',
    card_exp_year : '2015',
    csc:'999',
    amount:'100.00',
    email_address:'[email protected]',
    send_receipt   : true
]
 try    {
    resp = https.post(
    path:'/charge',
    requestContentType: URLENC,//request content synchronously for the     "headers"
    headers: ['Content-Type': 'application/x-www-form-urlencoded'],
    body:charge
)

} catch(HttpResponseException ex) {
    println ex
}
  println resp

the result:

groovyx.net.http.HttpResponseException: Not Found
null
0

1 Answer 1

6

Base URI was specified incorrectly - it should be hostname only - without any path. The following script works but fails with 401:

@Grab(group='org.codehaus.groovy.modules.http-builder',module='http-builder', version='0.7.1')
@Grab(group='org.slf4j', module='slf4j-log4j12', version='1.7.7')
@Grab(group='org.slf4j', module='jcl-over-slf4j', version='1.7.7')

import groovy.json.JsonOutput
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import static groovyx.net.http.ContentType.*


def username ='test_XXXXXXXXXXXXXXXXXXXX'

def https = new RESTClient('https://api.paymentspring.com/')
https.auth.basic(username, '')

def charge = [
    card_owner_name  : 'test tset',
    card_number      : '345829002709133',
    card_exp_month   : '12',
    card_exp_year    : '2015',
    csc              : '999',
    amount           : '100.00',
    email_address    : '[email protected]',
    send_receipt     : true,
]

try {
    def resp = https.post(
        path:'api/v1/charge',
        requestContentType: URLENC,
        headers: ['Content-Type': 'application/x-www-form-urlencoded'],
        body:charge,
    )
} catch(HttpResponseException e) {
    r = e.response
    println("Success: $r.success")
    println("Status:  $r.status")
    println("Reason:  $r.statusLine.reasonPhrase")
    println("Content: \n${JsonOutput.prettyPrint(JsonOutput.toJson(r.data))}")
} 
Sign up to request clarification or add additional context in comments.

1 Comment

While this does in fact work, you can also just take the / at the beginning of the path and add it to the URI and the original post would have also worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.