AWS API SDK出口Android不接受邮政上的body内容



我可以使用帖子来调用AWS API Gateway OK,当没有身体时。当有一个身体时,我会遇到此错误,其中9是字符串的长度:

com.amazonaws.mobileconnectors.apigateway.apiclientException:预期 0字节但收到9(服务:null;状态代码:0;错误代码: 无效的;请求ID:null(

当我使用邮递员时,我将车身html键/对作为身体/蓝色,效果很好。我想知道为什么Android的导出SDK不接受身体内容。感谢您的帮助。

我的代码是:

final MyAPIClient client = factory.build(MyAPIClient.class);
String body = "Body=BLUE";
byte[] content = body.getBytes("UTF-8");
ApiRequest request = new ApiRequest(client.getClass().getSimpleName())
                    .withPath("/vote")
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .withBody(content);
ApiResponse response = client.execute(request);

似乎您在请求标头表示您发送0字节数据时发送9个字节(因为Content-Length不设置(。

在验证您的请求时,服务器会意识到该事实,因此给您带来的错误。

从我的角度来看,您应该第二次致电addHeader并设置Content-Length

ApiRequest request = new ApiRequest(client.getClass().getSimpleName())
                    .withPath("/vote")
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("Content-Length", [insert byte count here])
                    .withBody(content);

有关Content-Length工作原理的更多信息,请参阅https://www.w3.org/protocols/rfc2616/rfc2616-sec14.html(滚动至14.13 content-length(

最新更新