Discord REST API 不接受 PATCH 请求方法

问题描述

使用以下代码:

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class Test2 {
    private static String token = "mytoken";
    private static String auth = null;
    public static void main(String[] args) {
        try {
            testDiscord();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void testDiscord() throws IOException {
        setAuthenticationToken(token);
        JSONObject data = new JSONObject();
        data.put("name","test2");
        Response response2 = readJson("https://discordapp.com/api/guilds/740975465468133557",REQUEST_METHOD.PATCH,data.toJSONString());
        System.out.println(response2.getResponseCode() + " : " + response2.getResponseMessage());
        System.out.println(response2.getData());
    }
    public static void setAuthenticationToken(String token) {
        auth = "Bot " + token;
    }
    public static Response readJson(String strUrl,REQUEST_METHOD method,String data) throws IOException {
        URL url = new URL(strUrl);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestProperty("Accept","application/json");
        con.setRequestProperty("Content-Type","application/json");
        con.setRequestProperty("User-Agent","DiscordBot (www.adriftus.com/,0.0.1)");
        con.setRequestProperty("Authorization",auth);
        if (method == REQUEST_METHOD.PATCH) {
            con.setRequestProperty("X-HTTP-Method-Override","PATCH");
            con.setRequestMethod("POST");
        } else {
            con.setRequestMethod(method.name());
        }
        con.setDoInput(true);
        if (data != null) {
            con.setDoOutput(true);
            try(OutputStream os = con.getOutputStream()) {
                byte[] input = data.getBytes("utf-8");
                os.write(input,input.length);
            } catch (IOException e) {
                throw e;
            }
        }
        System.out.println(con.getResponseCode());
        StringBuilder response = new StringBuilder();
        try {
            int statusCode = con.getResponseCode();
            InputStream is = null;
            if (statusCode >= 200 && statusCode < 400) {
                is = con.getInputStream();
            } else {
                is = con.getErrorStream();
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String responseLine = null;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
        } catch (IOException e) {
            throw e;
        }
        con.disconnect();
        Response r = new Response(con,response.toString());
        return r;
    }
    public static class Response {
        private Object data;
        private Map<String,List<String>> headers;
        private HttpURLConnection con;
        public Response(HttpURLConnection con,String data) {
            this.con = con;
            this.headers = con.getHeaderFields();
            try {
                this.data = JSONValue.parseWithException(data);
            } catch (ParseException e) {
                this.data = null;
            }
        }

        public int getResponseCode() {
            try {
                return this.con.getResponseCode();
            } catch (IOException e) {
                return 400;
            }
        }

        public String getResponseMessage() {
            try {
                return this.con.getResponseMessage();
            } catch (Exception e) {
                return null;
            }
        }

        public Object getData() {
            return this.data;
        }
        public Map<String,List<String>> getHeaders() {
            return this.headers;
        }
    }
    public enum REQUEST_METHOD {
        GET,POST,HEAD,OPTIONS,PUT,DELETE,PATCH;
    }

}

我的目标是将公会名称更改为 test2,但控制台输出是:

405 : Method Not Allowed
{"code":0,"message":"405: Method Not Allowed"}

此处 (https://discord.com/developers/docs/resources/guild#modify-guild) 文档说要使用 PATCH 方法,但 Discord 的 REST API 告诉我我不能对端点使用该方法。当我将方法更改为GET,但保留数据时;它给了我完全相同的错误。如果我使用 GET 并将数据留空,它会返回公会对象。如果我在没有数据的情况下使用 PATCH,则会出现以下错误:

411 : Length Required
null

注意:我无法为此项目使用第三方库(例如 Spring、Apache HTTPClient)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...