当前位置: 首页 > 知识库问答 >
问题:

改装收到400个错误请求,但与邮递员一起工作

东郭自强
2023-03-14

我的api基url是:

https://locodealapi.herokuapp.com/api/deals

在邮递员传球,跟随在头和它的工作很好。

x-access-token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX25hbWUiOiJkZWFsQWRtaW4iLCJ1c2VyX3Bhc3N3b3JkIjoiY2U5MDZkNDkzMWY3NmFhZWFmZTViNjM0YTg3Y2RkYzQ4YjMzNmUzYmVlZDU1N2ZhZTI0YzMxZTY3YmQyN2NhZDg4NDJmNzJlNTJlNGUyODc5ZWVkODY1MDljYzY5ODJlNDYwMjU0Mzk4ODQzMTljMTQxMGMwOTcxNGU0Y2MxMDNiMDQwYjkwOTQ1OTMyMjYwMGI1MjgwMmRkOTc2YTE4OGE5MTZiMWU4ZmU1YmY1ZWZiNTlkYjc2NDhmMjQxOTg0ODdiMWE5OGVhMjg2ZWZjZDI1M2Y5ZWUxNTQ0OTI3ODZiYjY3NzNmZTZjNGY3ODhmNzJjMzQ0NTEwMzg2NjkyNzIzMjNiMjZmYjFhMmUzYjE0ZTQ0Nzc0NzljMTExMzNkZjIwMmE3ODY2OGIxZjdiY2NlZjQ5NTM0YTJhYzk5N2Y0MTc0NzU2OTkwOTg5NjQ1YzljNWQxYTZkYmUyZTI1ZjFlNDE3YjdhYWI1N2QyZGJhNmVmOWEwNzk5N2Q5ZDcyNTgxMzUyYzc4Y2IxY2RiMDRmNDFkZjMwNzdjYzAxMGM2YmYzYWMzOTQ5OTJjN2Y3ZGVmYmE3YzllZWMxMzI5ODhlODBiM2RmYWE5NTVhMzE3NTgxNThiZjYxYjYyN2U4Y2UzOTg2NmRkODk2YzBlZDkzNDJlMzhiMzhkM2Q0ZTY0YWIzZWIwNGU4YmU5MGQwNmM2NTg2OWYyNzZkMzgyOTU3MTFkODgyNjI4NGIxMjkzYzQ0ZGNjMWM3ZjU2YmE4OGM5MTkyND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XdXdjcz0iLCJpYXQiOjE1MTAxMjY1OTUsImV4cCI6MTUxODc2NjU5NX0.5XFJnJqTsfID9uqOwkNf46oraj9jDxic7qNSqBdunD0
@POST("api/deals")
    Call<ResponseBody> deals(@Header("x-access-token") String x_access_token)
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://locodealapi.herokuapp.com")
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
        AppRestAPI client = retrofit.create(AppRestAPI.class);    

        Call<ResponseBody> call1 = client.deals(
                token
        );          

        call1.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.i(TAG, "The response is " + response.message());
                Log.i(TAG, "The response is " + response.body());    

                try {
                    Log.i(TAG, "The response is " + response.errorBody().string());

                    if (response.code() == 400) {
                        Log.v("Error code 400",response.errorBody().string());
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }    
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "onFailure: Failed", t);

            }
        });

OKHTTP日志拦截器

11-21 21:14:30.939 3253-3342/? D/OkHttp: --> POST https://locodealapi.herokuapp.com/api/deals
11-21 21:14:30.939 3253-3342/? D/OkHttp: Content-Length: 0
11-21 21:14:30.940 3253-3342/? D/OkHttp: x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyNSwidXNlcl9uYW1lIj....
11-21 21:14:30.942 3253-3342/? D/OkHttp: --> END POST (0-byte body)    
11-21 21:14:34.188 3253-3342/? D/OkHttp: <-- 400 Bad Request https://locodealapi.herokuapp.com/api/deals (3246ms)
11-21 21:14:34.189 3253-3342/? D/OkHttp: Connection: close
11-21 21:14:34.189 3253-3342/? D/OkHttp: Server: Cowboy
11-21 21:14:34.189 3253-3342/? D/OkHttp: Date: Tue, 21 Nov 2017 15:29:33 GMT
11-21 21:14:34.190 3253-3342/? D/OkHttp: Content-Length: 0
11-21 21:14:34.191 3253-3342/? D/OkHttp: <-- END HTTP (0-byte body)
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is Bad Request
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is null
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is 

注意:相同的代码在本地nodejs服务器(仅限http)中运行良好

共有1个答案

弓宏茂
2023-03-14

1.api首先是一个GET方法,所以使用@GET代替@post

其次,尝试将reverfit.baseurl(“https://locodealapi.herokuapp.com”)中的url基url更改为.baseurl(“https://locodealapi.herokuapp.com/”),这样就可以了。或者将您的问题留在注释2中。这是示例代码

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(
            new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException{
                 Request original = chain.request();
                    // Request customization: add request headers
                    Request.Builder requestBuilder =original.newBuilder().
                            method(original.method(), original.body());
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            })
            .addInterceptor(interceptor).connectTimeout(60,TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();

    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://locodealapi.herokuapp.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient).build();
    UserApi userApi = retrofit.create(UserApi.class);
    Call<ResponseBody> call = userApi.deals("your token");
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,retrofit2.Response<ResponseBody> response) {
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {}
    });
}

@get(“api/deals”)调用交易(@header(“x-access-token”)字符串x_access_token);

 类似资料:
  • 我正在使用volley库我的volley请求在牛轧糖,奥利奥,棉花糖操作系统工作完美,但它不工作在Lollipop设备,它给出错误服务器错误,我处理了错误,我发现错误是400坏请求,我正在分享错误屏幕截图,也分享了我的代码,请帮助我,我的api也工作在邮递员... 我的请求代码是:

  • 我正试着在邮递员上打API。API正在服务器上工作,但我没有收到任何数据。以下是视觉图像: 它是回应我与“没有数据”和400错误的要求。

  • 我正在开发一个Registraion API,它接受以下/request perameters的json输入 但是当我从android应用程序调用时,它给出了错误的响应,错误代码为,但在Postman上工作得很好。 我的API客户端 衷心感谢

  • 我正在尝试使用Postman访问API,以使用基本身份验证获取响应,但当我提交数据时,它会给我带来可怕的400错误,这显然表明某些标头设置不正确。 以下是API信息: 作为回应,我应该得到一个JSON形式的加密令牌,而不是得到这个错误。 以下是邮差截图: 我错过了什么吗?

  • 在我的Laravel应用程序中,我需要定期使用Guzzle将数据发送到API。 API使用承载令牌进行身份验证,并请求和接受原始JSON。为了进行测试,我使用Postman访问了API,一切都工作得很好。

  • 我有一个非常基本的webApi构建/运行(相当模板应用程序,仅此而已),以便遵循一个培训视频。GET和DELETE调用工作正常,PUT和POST给出400错误。诚然,对所有这些都很新,所以我正在寻找一些基本的方向,关于如何排除故障。相关的屏幕截图如下,我很高兴提供其他要求。提前谢了。