Consume Opentext Rest API using Groovy

I need to create a group in opentext using their Rest API using Groovy: enter image description here

So I tried to use this code

    @Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.2')

import groovy.json.*
import org.apache.http.client.methods.*
import org.apache.http.entity.*
import org.apache.http.impl.client.*

// build JSON
def map = [:]
map["type"] = 1
map["name"] = "ThisIsAGroup"
def jsonBody = new JsonBuilder(map).toString()

// build HTTP POST
String auth = "login:Password";
String encoding = auth.bytes.encodeBase64().toString()
def url = 'https://link_of_my_server/api/v2/members'
def post = new HttpPost(url)
post.setHeader("Authorization", "Basic " + encoding);
post.addHeader("content-type", "application/json")
post.setEntity(new StringEntity(jsonBody))

// execute 
def client = HttpClientBuilder.create().build()
def response = client.execute(post)
def bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))
def jsonResponse = bufferedReader.getText()

unfortunately, it shows me this error message : enter image description here

with Response Code = 400

PS: When I try to consume a "Get/ PUT/ DELETE" method , this piece of code works like magic ! the problem appears only with the "POST"

Read more here:

Content Attribution

This content was originally published by maryem neyli at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: