Android POST请求从AWS API Gateway返回403

时间:2017-04-30 04:28:38

标签: android amazon-web-services post aws-lambda aws-api-gateway

我正在使用AWS Lambda w / API网关触发器,我遇到了一个问题。

我正在使用RetroFit发出POST请求,我的回复收到了403。我正在传递带有URL的JSON,其中亚马逊服务将使用它做一些事情。我需要在标题中添加一些内容吗?我从请求中删除了授权。

这是原始邮件,它没有给我详细的消息。

“响应{protocol = h2,code = 403,message =,url = https://eun533ayha.execute-api.us-west-1.amazonaws.com/lio}”

接口/改装服务

public interface AmazonService {
    @Headers("Content-Type: application/json; charset=utf8")
    @POST("/lio")
    Observable<ResponseBody> getAmazonResponse(@Body JSONObject input);
}
amazonRetrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .callbackExecutor(executor)
            .client(okHttpClient)
            .baseUrl("https://eun533ayha.execute-api.us-west-1.amazonaws.com/Prod/")
            .build();


AmazonService as = amazonRetrofit.create(AmazonService.class);
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("url", "www.google.com");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Observable<ResponseBody> obs = as.getAmazonResponse(jsonObject);

    obs.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<ResponseBody>() {
                @Override
                public void onCompleted() {
                    Log.d(TAG, "response onCompleted()");
                }

                @Override
                public void onError(Throwable throwable) {
                    Log.d(TAG, "response onError()");
                }

                @Override
                public void onNext(ResponseBody response) {
                    Log.d(TAG, "response onNext()");
                }
            });

1 个答案:

答案 0 :(得分:1)

我认为您正在使用Retrofit 2,而Retrofit 2使用相同的规则。

如果你的路径上有一个前导'/',它将被视为绝对路径。

public interface AmazonService {
    @Headers("Content-Type: application/json; charset=utf8")
    @POST("/lio")
    Observable<ResponseBody> getAmazonResponse();
}

amazonRetrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("https://eun533ayha.execute-api.us-west-1.amazonaws.com/Prod/")
        .build();

结果:https://eun533ayha.execute-api.us-west-1.amazonaws.com/lio

如果删除路径上的前导'/',则会将其视为相关路径。

public interface AmazonService {
    @Headers("Content-Type: application/json; charset=utf8")
    @POST("lio")
    Observable<ResponseBody> getAmazonResponse();
}

amazonRetrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("https://eun533ayha.execute-api.us-west-1.amazonaws.com/Prod/")
        .build();

结果:https://eun533ayha.execute-api.us-west-1.amazonaws.com/Prod/lio

在您的情况下,看起来您有一个舞台'Prod'并且您的集成在资源'/ lio'上。我认为如果删除前导'/'。

,它会起作用