我在用Retrofit2。发送一个带有主体的POST请求(作为JSON数组),但得到的是“400 Bad request”。同时,从SoapUI和我的http客户机(基于HttpURLConnection)发送请求也很好。
public interface ApiService {
@POST( "let/fmir" )
@Headers( {
"Accept-Encoding: gzip,deflate",
"Content-Type: Application/Json;charset=UTF-8",
"Accept: Application/Json",
"User-Agent: Retrofit 2.3.0"
} )
Call<LetPOJO> fmir(
@Header( "Authorization" ) String authorization,
@Body String body
);
}
public class ApiServiceProvider {
public ApiService createApiService() {
// Prepare the Retrofit logger
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor( interceptor).build();
// Build the Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl( "http://1.1.1.1:3040/api/v1.0/" )
.client( client )
.addConverterFactory( JacksonConverterFactory.create() );
.build();
return retrofit.create( ApiService.class );
}
}
ApiService apiService = ApiServiceProvider.createApiService();
Call<LetPOJO> call = apiService.fmir(
"Basic NDUhg4234OQ==",
"[\"L___1\",\"L___2\",\"L___3\",\"L___4\"]"
);
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: --> POST http://1.1.1.1:3040/api/v1.0/let/fmir
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: Content-Type: Application/Json;charset=UTF-8
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: Content-Length: 139
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: Accept-Encoding: gzip,deflate
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: Accept: Application/Json
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: User-Agent: Retrofit 2.3.0
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: Authorization: Basic NDUhg4234OQ==
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO:
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: "[\"L___1\",\"L___2\",\"L___3\",\"L___4\"]"
янв 24, 2018 10:12:38 AM okhttp3.internal.platform.Platform log INFO: --> END POST (139-byte body)
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: <-- 400 Bad Request http://1.1.1.1:3040/api/v1.0/let/fmir (121ms)
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Vary: Accept-Encoding
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Content-Encoding: gzip
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Content-Type: text/plain
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Date: Wed, 24 Jan 2018 07:12:39 GMT
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Connection: close
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: Content-Length: 192
янв 24, 2018 10:12:39 AM okhttp3.internal.platform.Platform log INFO: <-- END HTTP (encoded body omitted)
响应的内容类型是text/plain,但应该是application/json
例如,使用SOAPUI的请求/响应:请求:
POST http://1.1.1.1:3040/api/v1.0/let/fmir HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json;charset=UTF-8
Authorization: Basic NDUhg4234OQ==
Content-Length: 129
Host: 1.1.1.1:3040
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
及其回应:
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 24 Jan 2018 08:33:12 GMT
Content-Length: 689
[{"id":"L___1"}]
这是服务器endpoint:
@ApiOperation("Some description")
@POST @Path("fmir")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "ОК"),
@ApiResponse(code = 400, message = "some message"),
@ApiResponse(code = 500, message = "some message")
})
public List<Letter> getLet(
@ApiParam(value = "Authorization header")
@HeaderParam(HttpHeaders.AUTHORIZATION) final String authorization,
@Context ContainerRequestContext context,
final List<String> letterIds) {
// ...
}
由于服务器endpoint需要一个字符串列表,我认为您的客户机也需要发送这个列表(not send a string):
所以,试试这个:
public interface ApiService {
@POST( "let/fmir" )
@Headers( {
"Accept-Encoding: gzip,deflate",
"Content-Type: Application/Json;charset=UTF-8",
"Accept: Application/Json",
"User-Agent: Retrofit 2.3.0"
} )
Call<LetPOJO> fmir(
@Header( "Authorization" ) String authorization,
@Body List<String> body
);
}
...
ApiService apiService = ApiServiceProvider.createApiService();
Call<LetPOJO> call = apiService.fmir(
"Basic NDUhg4234OQ==",
Arrays.asList("L___1", "L___2", "L___3", "L___4")
);
当试图将我的JSON映射到Spring MVC控制器中的Java对象时,我在Ajax请求上收到了400个错误请求。我已经检查了主题中的大部分相关问题,但仍然无法使其工作 Ajax调用和JSON: 我的控制器: 我的Java对象: 我使用Spring 4.2.5和Jackson: 我遇到的错误: HTTP错误400访问/ux/词汇表/createVocationary时出现问题。原因:糟糕的请求 此
因此,当我对服务器进行POST API调用时,我得到一个带有JSON响应的400 Bad Request错误。 我管它叫 然而,问题是,一旦我得到响应,就调用onFailure(),这样就调用了//bb。在这里,我无法访问JSON响应。当我记录api请求和响应时,它根本不显示JSON响应。而可抛出的t是IOException。然而,奇怪的是,当我对Postman进行相同的调用时,它确实返回了预期的
我试图使用Jquery发送一个Ajax POST请求,但我有400个错误请求。 这是我的代码: 它说:无法根据请求构建资源。我错过了什么?
我正在开发一个Registraion API,它接受以下/request perameters的json输入 但是当我从android应用程序调用时,它给出了错误的响应,错误代码为,但在Postman上工作得很好。 我的API客户端 衷心感谢
我在Eclipse Photon上用Java8编写了一个简单的webService,使用RestTemplate发布(使用postForObject)一个对象(称为patentListWrapper),该对象包装了一个对象列表(称为PatentDetails)。我从Java客户机(称为MainWsClient)发布,然后在服务器端的patentDetails中设置一个值,并在客户机中读取paten
我想向服务器发送一篇帖子,帖子的正文是真是假。我有这个代码,我使用截击库 ShoozyHeader()将内容类型设置为text/plain,并将Accept设置为text/plain以及身份验证所需的其他标头。 如果我尝试http://requestmaker.com/,服务器正确响应,但我运行此代码,服务器响应: 错误的请求-无效的头HTTP错误400。请求的标头名称无效。 如果删除getBod