trello API“PUT / 1 /卡/ [卡ID] / desc”的返回400响应与消息“无效

2019-10-29 20:26发布

我已经花一个星期试图找出如何更新一些卡的信息,我希望在一个,例如姓名,递减,IDLIST,关闭等更新领域的载荷,但环顾四周之后,似乎他们要单独完成,但是当我尝试我不断收到400响应与消息“不存在价值的价值。”

例如,当我试图把https://api.trello.com/1/cards/[cardid]/desc?key=[mykey]&token=[mytoken]value=just+ye​​t+another+test+of+trello+侧+扩展

我究竟做错了什么?

使用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());
    }
}

Answer 1:

我发现我失踪的一行代码

            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());
    }
}


Answer 2:

基于您的代码:

sb.append(sb.length() > 0 ? "&" : "")
                .append(key)
                .append("=")
                .append(URLEncoder.encode(map.get(key), "UTF-8"));

它看起来像你实际上构建一个URL,看起来像:

https://api.trello.com/1/cards/[cardid]/desc?key=[mykey]&token=[mytoken]value=just+yet+another+test+of+trello+side+extended

需要注意的是,没有&之间tokenvalue参数。



文章来源: trello api “PUT /1/cards/[card id]/desc” returns a 400 response with message “invalid value for value”
标签: java api trello