【发布时间】:2021-04-08 16:35:56
【问题描述】:
我需要使用他们的 Rest API 使用 Groovy 在 opentext 中创建一个组:
所以我尝试使用这段代码
@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()
响应代码 = 400
PS:当我尝试使用“Get/PUT/DELETE”方法时,这段代码就像魔术一样工作!问题只出现在“POST”
【问题讨论】: