RestTemplate DELETE引发HttpClientErrorException:405不允许使用方法



我正在为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;
        }
    }

URL_INTENTS值似乎不正确:根据您上面引用的文档,您应该点击URL /intents/{intent-id}。但是,基于https://api.api.ai/v1/intents?v={apiVersion}的值,您没有指定要删除的意图的ID。因此,当服务器找到根/intents资源时,它只是禁止您删除该根资源。

相关内容

  • 没有找到相关文章

最新更新