Android POST请求从AWS API网关返回403 [英] Android POST Request returning 403 from AWS API Gateway

查看:82
本文介绍了Android POST请求从AWS API网关返回403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带API网关触发器的AWS Lambda,但遇到了问题.

I'm using AWS Lambda w/ API gateway triggers and I am running into an issue.

我正在使用RetroFit发出POST请求,并且得到403的回复.我正在传递一个带有URL的JSON,其中Amazon服务将对其进行处理.我需要在标题中添加一些内容吗?我从请求中删除了授权.

I'm using RetroFit to make a POST request and I'm getting a 403 for my response back. I am passing a JSON with a URL where the amazon service will do something with it. Is there something I need to put in the headers? I removed authorization from the request.

这是原始消息,没有给我详细的消息.

Here is the raw message and it doesn't give me a detailed message.

"Response {protocol = h2,code = 403,message =,url =

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

推荐答案

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

I think you are using Retrofit 2, and Retrofit 2 uses the same rules as .

如果您的路径上有一个前导"/",它将被视为绝对路径.

If you have a leading '/' on your path, it will consider as absolute path.

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

如果您删除路径中的前导"/",它将被视为相关路径.

If you remove the leading '/' on your path, it will consider as related path.

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"上.我认为,如果删除开头的"/",它会起作用.

In your case, it looks like that you have a stage 'Prod' and your integration is on the resource '/lio'. I think it will work if you remove the leading '/'.

这篇关于Android POST请求从AWS API网关返回403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆