我正在编写https://api.ai服务的客户端应用程序,并使用org.springframework.web.client.RestTemplate来处理我的HTTP请求。
问题是DELETE请求总是返回错误405,
private static final String URL_INTENTS = "https://api.api.ai/v1/intents?v={apiVersion}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
httpHeaders.set("Authorization", "Bearer " + speaktoitConfig.getDeveloperAccessToken());
httpHeaders.set("ocp-apim-subscription-key", speaktoitConfig.getSubscriptionKey());
resp = restTemplate.exchange(
URL_INTENTS, HttpMethod.DELETE, new HttpEntity<String>(intentId, httpHeaders),
AiVocabulariesResponse.class, speaktoitConfig.getApiVersion()).getBody();
但PUT和GET请求正常。
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
httpHeaders.set("Authorization", "Bearer " + speaktoitConfig.getDeveloperAccessToken());
httpHeaders.set("ocp-apim-subscription-key", speaktoitConfig.getSubscriptionKey());
resp = restTemplate.exchange(
URL_INTENTS, HttpMethod.PUT, new HttpEntity<String>(JsonMapper.MAPPER.writeValueAsString(aiQueryIntentsList), httpHeaders),
AiVocabulariesResponse.class, speaktoitConfig.getApiVersion());
我非常确定api.ai支持DELETE,因为它是在他们的文档中编写的 https://docs.api.ai/docs/intents
您认为我的代码存在问题,或者我应该写信给api.ai服务支持?
这是一个完整的测试代码
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class TestDeleteRequest {
@Test
public void test1DeleteRequest() {
Assert.assertTrue(testDeleteRequest());
}
public static final ObjectMapper MAPPER = new ObjectMapper();
static boolean testDeleteRequest() {
final RestTemplate restTemplate = new RestTemplate();
final String URL_INTENTS = "https://console.api.ai/v1/intents?v={apiVersion}";
final String API_VERSION = "20150901";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
httpHeaders.set("Authorization", "Bearer " + "fc361168a9434289b7457408fa0acdd8");
httpHeaders.set("ocp-apim-subscription-key", "76dba252-3e56-488d-9350-22f32b3b55f0");
//download all elements headers
String respStr = null;
try {
respStr = restTemplate.exchange(
URL_INTENTS, HttpMethod.GET, new HttpEntity<String>("", httpHeaders),
String.class, API_VERSION).getBody();
} catch (Throwable e) {
return false;
}
//parse elements ids
List<String> intentIds = new LinkedList<>();
try {
int cnt = 1;
JsonNode rootNode = MAPPER.readTree(respStr);
Iterator<JsonNode> elements = rootNode.elements();
while (elements.hasNext()) {
JsonNode aiIntentNode = elements.next();
JsonNode aiIntentIdNode = aiIntentNode.path("id");
String aiIntentId = aiIntentIdNode.asText();
intentIds.add(aiIntentId);
}
} catch(Exception e) {
return false;
}
if (intentIds.isEmpty()) {
return false;
}
//try to delete the first one
try {
restTemplate.exchange(
URL_INTENTS, HttpMethod.DELETE, new HttpEntity<String>(intentIds.get(0), httpHeaders),
String.class, API_VERSION).getBody();
} catch (Throwable e) {
return false; //TODO catch HttpClientErrorException 405 Method no Allowed
}
return true;
}
}
答案 0 :(得分:0)
URL_INTENTS
值似乎不正确:根据您在上面引用的文档,您应该点击网址/intents/{intent-id}
。但是,根据您的https://api.api.ai/v1/intents?v={apiVersion}
值,您没有指定要删除的意图的ID。因此,当服务器找到根/intents
资源时,它只是禁止您删除该根资源。