java - trello api "PUT/1/cards/[card id]/desc"返回带有消息 "invalid value for value"的 400 响应

标签 java api trello

我花了一周的时间试图弄清楚如何更新一些卡信息,我希望一次更新大量字段,例如name、desc、idList、close 等,但环顾四周后,似乎它们必须单独完成,但当我尝试时,我不断收到 400 响应,并显示消息“值的值无效”。

例如当我尝试 PUT https://api.trello.com/1/cards/[cardid]/desc?key=[mykey]&token=[mytoken]value=just+ye​​t+another+test+of+trello+side+extended

我做错了什么?

用于发送Put的Java代码是

private static InputStream doRequest(final String url, final String requestMethod, final Map<String, String> map) 
{
    try 
    {
        final HttpsURLConnection conn = (HttpsURLConnection) new URL(url)
                .openConnection();
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        conn.setDoOutput(requestMethod.equals(METHOD_POST) || requestMethod.equals(METHOD_PUT));
        conn.setRequestMethod(requestMethod);

        String plus = "";
        if (map != null && !map.isEmpty()) 
        {
            final StringBuilder sb = new StringBuilder();
            for (String key : map.keySet()) 
            {
                sb.append(sb.length() > 0 ? "&" : "")
                    .append(key)
                    .append("=")
                    .append(URLEncoder.encode(map.get(key), "UTF-8"));
            }
            conn.getOutputStream().write(sb.toString().getBytes());
            conn.getOutputStream().close();
            plus = sb.toString();
        }
        final int rc = conn.getResponseCode();
        logger.info("response " + rc + " from " + requestMethod + " " + url + plus);
        if (rc > 399) 
        {
             return null;
        } 
        else 
        {
            return getWrappedInputStream(
                conn.getInputStream(), GZIP_ENCODING.equalsIgnoreCase(conn.getContentEncoding())
            );
        }
    } 
    catch (IOException e) 
    {
        throw new TrelloException(e.getMessage());
    }
}

最佳答案

我发现代码中少了一行

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

解决了问题,所以现在是 doRequest 的完整代码...

    private static InputStream doRequest(final String url, final String requestMethod, final Map<String, String> map) 
{
    try 
    {
        final HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        conn.setDoOutput(requestMethod.equals(METHOD_POST) || requestMethod.equals(METHOD_PUT));
        conn.setRequestMethod(requestMethod);
        // following line was missing and caused PUT not to work.
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        String arguments = "";
        if (map != null && !map.isEmpty()) 
        {
            final StringBuilder sb = new StringBuilder();
            for (String key : map.keySet()) 
            {
                sb.append(sb.length() > 0 ? "&" : "");
                sb.append(URLEncoder.encode(key, HTTP_CHARACTER_ENCODING));
                sb.append("=");
                sb.append(URLEncoder.encode(map.get(key), HTTP_CHARACTER_ENCODING));
            }
            conn.getOutputStream().write(sb.toString().getBytes());
            conn.getOutputStream().close();
            arguments = sb.toString();
        }
        conn.connect();

        final int rc = conn.getResponseCode();
        final String responseMessage = conn.getResponseMessage();
        if (rc != 200)
            logger.info("response " + rc + " (" + responseMessage + ") from " + requestMethod + " " + url + " args:" + arguments);
        if (rc > 399) 
        {
            final String str = stream2String(conn.getErrorStream());
            logger.info("error response:" + str);
            return null;
        } 
        else 
        {
            return getWrappedInputStream(
                conn.getInputStream(), GZIP_ENCODING.equalsIgnoreCase(conn.getContentEncoding())
            );
        }
    } 
    catch (IOException e) 
    {
        throw new TrelloException(e.getMessage());
    }
}

关于java - trello api "PUT/1/cards/[card id]/desc"返回带有消息 "invalid value for value"的 400 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867336/

相关文章:

trello - Trello webhook 问题

python - django api 用于在数据库中创建数据

java - 内在函数和内联对 Lambda 性能的影响?

java - 注销时如何清除android中的缓存?

java - Hadoop 生成多个 VM

rest - 如何为NFL Shield API创建访问 token ?

java - 将微秒时间转换为毫秒时间

javascript - 运行 google apps 脚本 (UrlFetchApp) 时出现 "Window not defined"错误

javascript - 无法从 Microsoft Office JavaScript 加载项向 Trello 进行身份验证

java - 如何从listfragment java类进入其他 Activity