Java 特雷罗api“;放置/1/cards/[card id]/desc“;返回一个400响应,其中包含消息";值“”的值无效;
我花了一个星期的时间试图解决如何更新一些卡片信息,我希望在一个位置更新一堆字段,例如name、desc、idList、closed等,但环顾四周后,它们似乎必须单独完成,但当我尝试时,我不断收到400条回复,并显示消息“valueforvalue无效” e、 当我试图 放https://api.trello.com/1/cards/[cardid]/desc?key=[mykey]&token=[mytoken]value=just+yet+other+test+of+trello+side+extended 我做错了什么 用于发送Put的Java代码是Java 特雷罗api“;放置/1/cards/[card id]/desc“;返回一个400响应,其中包含消息";值“”的值无效;,java,api,trello,Java,Api,Trello,我花了一个星期的时间试图解决如何更新一些卡片信息,我希望在一个位置更新一堆字段,例如name、desc、idList、closed等,但环顾四周后,它们似乎必须单独完成,但当我尝试时,我不断收到400条回复,并显示消息“valueforvalue无效” e、 当我试图 放https://api.trello.com/1/cards/[cardid]/desc?key=[mykey]&token=[mytoken]value=just+yet+other+test+of+trello+side+e
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());
}
}
私有静态InputStream doRequest(最终字符串url、最终字符串requestMethod、最终映射)
{
尝试
{
最终HttpsURLConnection连接=(HttpsURLConnection)新URL(URL)
.openConnection();
conn.setRequestProperty(“接受编码”、“gzip、deflate”);
conn.setDoOutput(requestMethod.equals(METHOD_POST)| | requestMethod.equals(METHOD_PUT));
conn.setRequestMethod(requestMethod);
字符串加上“”;
if(map!=null&&!map.isEmpty())
{
最终StringBuilder sb=新StringBuilder();
for(字符串键:map.keySet())
{
sb.append(sb.length()>0?&“:”)
.append(键)
.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(“来自“+requestMethod+”“+url+plus”的响应“+rc+”);
如果(rc>399)
{
返回null;
}
其他的
{
返回getWrappedInputStream(
conn.getInputStream(),GZIP_ENCODING.equalsIgnoreCase(conn.getContentEncoding())
);
}
}
捕获(IOE异常)
{
抛出新的TrelloException(例如getMessage());
}
}
根据您的代码:
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
请注意,
标记
和值
参数之间没有&
。我发现代码中缺少一行
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());
}
}
私有静态InputStream doRequest(最终字符串url、最终字符串requestMethod、最终映射)
{
尝试
{
最终的HttpsURLConnection连接=(HttpsURLConnection)新URL(URL).openConnection();
conn.setRequestProperty(“接受编码”、“gzip、deflate”);
conn.setDoOutput(requestMethod.equals(METHOD_POST)| | requestMethod.equals(METHOD_PUT));
conn.setRequestMethod(requestMethod);
//以下线路缺失,导致PUT无法工作。
conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
字符串参数=”;
if(map!=null&&!map.isEmpty())
{
最终StringBuilder sb=新StringBuilder();
for(字符串键:map.keySet())
{
sb.append(sb.length()>0?&“:”);
sb.append(URLEncoder.encode(key,HTTP_字符编码));
某人加上(“=”);
sb.append(URLEncoder.encode(map.get(key)、HTTP_字符_编码));
}
conn.getOutputStream().write(sb.toString().getBytes());
conn.getOutputStream().close();
参数=sb.toString();
}
连接();
final int rc=conn.getResponseCode();
最终字符串responseMessage=conn.getResponseMessage();
如果(rc!=200)
logger.info(“response”+rc+”(“+responseMessage+”)来自“+requestMethod+”“+url+”参数:“+arguments”);
如果(rc>399)
{
最后一个字符串str=stream2String(conn.getErrorStream());
logger.info(“错误响应:+str”);
返回null;
}
其他的
{
返回getWrappedInputStream(
conn.getInputStream(),GZIP_ENCODING.equalsIgnoreCase(conn.getContentEncoding())
);
}
}
捕获(IOE异常)
{
抛出新的TrelloException(例如getMessage());
}
}
了解400所附带的信息可能会有所帮助。还有,有什么原因不能同时更新多个字段吗?我不知道如何获取响应消息。是的,我也试过用你说的那个,但也没用。你用什么来提出这个请求?尝试curl-i-X PUThttps://api.trello.com/1/cards/[cardd]/desc?key=[mykey]&token=[mytoken]value=just+yet+other+test+of+trello+side+extended
并获取输出。这应该包括消息。我是从java来做的。不知道什么是curl,如果你在OS X或Linux上,它可能已经安装好了,你可以将它粘贴到终端上。密钥和令牌被添加到传递的url中in@user2790622来自PUT@user2790622的logger.info
行的响应400(错误请求)的输出是什么。让我知道这是否有帮助。尝试过(有和没有“&”before值)没有区别