GROOVY RESTClient: No encoder found for request content type */*

11,316

Solution 1

Refer docs on http-builder. Specifically,

Since we never set a default content-type on the RESTClient instance or pass a contentType argument in this request, RESTClient will put Accept: / in the request header, and parse the response based on whatever is given in the response content-type header.

Modify, post() call as below:

@Grab('org.codehaus.groovy.modules.http-builder:'http-builder:0.7' )
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*

def client = new RESTClient( 'http://localhost' )
def resp = client.post( 
    path: '/services/adi/validateadimeta/fromfile',
    body : [ file : 'foo' ],
    requestContentType : JSON 
)

Solution 2

When i add "requestContentType : JSON", it returned "Bad request" It's changed to "requestContentType: URLENC", it works for me.

Read documentation of RESTClient it explained for me

Note that the above example is posting the request data as application/x-www-form-urlencoded. (The twitter API doesn't support XML or JSON POST requests.) For this reason, a requestContentType parameter must be specified in order to identify how the request body should be serialized.

Since we never set a default content-type on the RESTClient instance or pass a contentType argument in this request, RESTClient will put Accept: / in the request header, and parse the response based on whatever is given in the response content-type header. So because Twitter correctly identifies its response as application/json, it will automatically be parsed by the default JSON parser.

Share:
11,316
user2475310
Author by

user2475310

Updated on June 27, 2022

Comments

  • user2475310
    user2475310 almost 2 years

    I am running a rest POST request and I am getting this error when I compile:

    Caught: java.lang.IllegalArgumentException: No encoder found for request content type */*
    

    Here is my code:

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
    import groovyx.net.http.RESTClient
    def client = new RESTClient( 'http://localhost' )
    def resp = client.post( path : '/services/adi/validateadimeta/fromfile',body : [ file:'foo' ] )
    

    I am not sure if its responding or not maybe its a rencoding problem with the response? The */* has me concerned that its not even making a connection. When I run this as a CURL command on the commandline it works fine. file is the only parameter needed for this post call.

    Thanks