用于get请求抛出的Groovy脚本405



我正在向第三方Mule API发送一个请求,但它抛出了一个405。我不确定代码中出了什么问题。

这是我的代码

loggerApi.info("----------------------------------------Part1 scripttask6 - GET check API - start--------------------------------------")
//-------GET check API-------->
import groovy.json.JsonSlurper;
import javax.xml.bind.DatatypeConverter;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import java.nio.charset.StandardCharsets;

def apiCall = new URL('https://api-stage.com/api/v1/users/check').openConnection();
url=execution.getVariable('serviceUrlCheck')
def apiCall = new URL(url).openConnection();
def emailresult = "userMail=" + String.valueOf(UserEmail2)
def valueresult = "adGroup=" + String.valueOf(BusinessValuePassed)
loggerApi.info("emailresult is ${emailresult}")
loggerApi.info("valueresult is ${valueresult}")
def message = "?" +String.valueOf(emailresult) +"&" +String.valueOf(valueresult)
loggerApi.info("message is ${message}")
loggerApi.info("about to execute setRequestMethod to GET")
apiCall.setRequestMethod("GET")
apiCall.setRequestProperty("cache-control", "no-cache")
apiCall.setRequestProperty("Content-Type", "application/json")
apiCall.setRequestProperty("client_id", client_id)
apiCall.setRequestProperty("client_secret", client_secret)
apiCall.setDoOutput(true)
loggerApi.info("apiCall is ${apiCall}")
apiCall.getOutputStream().write(message.getBytes("UTF-8"));

loggerApi.info("getting response ${apiCall}")
def apiCallRes = apiCall.getResponseMessage();
loggerApi.info("apiCall res : " + apiCallRes);
def apiCallRC = apiCall.getResponseCode();
loggerApi.info("HTTP Response: " + apiCallRC);
if(apiCallRC.equals(200)) {
loggerApi.info("Response is ::: ${apiCallRes}");
loggerApi.info("Requester Found in AD-Group")
execution.setVariable("apiCallRC",apiCallRC.toString())
}  else if(apiCallRC.equals(400)){
loggerApi.info("Response is ::: ${apiCallRes}");
loggerApi.info("Requester not found in AD-Group")
execution.setVariable("apiCallRC",apiCallRC.toString())
}  else if(apiCallRC.equals(404)){
loggerApi.info("Response is ::: ${apiCallRes}");
loggerApi.info("Bad Requester")
execution.setVariable("apiCallRC",apiCallRC.toString())
}  else if(apiCallRC.equals(401)){
loggerApi.info("Response is ::: ${apiCallRes}");
loggerApi.info("API Call Credentials not valid")
execution.setVariable("apiCallRC",apiCallRC.toString())
}  else {
loggerApi.info("Bad Call")
execution.setVariable("apiCallRC",apiCallRC.toString())
}
loggerApi.info("apiCallRC string value is ${apiCallRC}")

错误响应:

----------------------------------------Part1 scripttask6 - GET check API - start--------------------------------------
2021-06-03 16:47:59.114 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - emailresult is userMail=xyz@xyz.com
2021-06-03 16:47:59.114 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - valueresult is adGroup=abcd-efgh
2021-06-03 16:47:59.114 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - message is ?userMail=xyz@xyz.com&adGroup=abcd-efgh
2021-06-03 16:47:59.114 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - about to execute setRequestMethod to GET
2021-06-03 16:47:59.114 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - apiCall is sun.net.www.protocol.https.DelegateHttpsURLConnection:https://api-stage.com/api/v1/users/check
2021-06-03 16:47:59.433 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - getting response sun.net.www.protocol.https.DelegateHttpsURLConnection:https://api-stage.com/api/v1/users/check
2021-06-03 16:47:59.487 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - apiCall res : Method Not Allowed
2021-06-03 16:47:59.488 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - HTTP Response: 405
2021-06-03 16:47:59.488 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - Bad Call
2021-06-03 16:47:59.488 [https-jsse-nio-0.0.0.0-5400-exec-40] INFO  c.c.d.c.api.component.LoggerApiImpl - apiCallRC string value is 405
2021-06-03 16:47:59.492 [https-jsse-nio-0.0.0.0-5400-exec-40] WARN  c.c.d.c.w.a.bean.AbstractUsersBean - User expression: xxx did not resolve to a user

更改

apiCall.setRequestMethod("GET")

apiCall.setRequestMethod("POST")

我想,如果您在正文中以JSON的形式发送请求,通常是通过POST发送的。

最新更新