Apache HttpClient 进行 Post 请求 异常问题( Java 对接 walmart mp)

查看 74|回复 4
作者:RedBeanIce   
(由于请求 walmart 有账号密码,属于公司保密信息,所以无法提供实际代码)
问题:请求异常只体现在日志中,如下面的第三行日志就是日志,有体现 carrier 字段是错误的。
如果在 response 里面获取,只能获取到 bad request ,没有具体的某个字段的错误。
请问,我如何获取到日志里面的报错,然后在自己写的代码里面获取到这个报错,自己打印出来。
1 ,
```
请求框架是,Apache HttpClient
org.apache.httpcomponents
4.5.6
HttpClient
```
```
17:33:50.629 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: close[\r][\n]"
17:33:50.629 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
17:33:50.629 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{"errors":{"error":[{"code":"INVALID_REQUEST_CONTENT.GMP_ORDER_API","field":"carrier","description":"Unsupported carrier 'Fedex' was specified in line number 1. Only pre-defined names are allowed for 'carrier.' For a list of acceptable carriers, see https://developer.walmart.com/api/us/mp/orders#operation/shippingUpdates . If you select 'otherCarrier' you must also provide the tracking URL.","info":"Request content is invalid.","severity":"ERROR","category":"DATA","errorIdentifiers":{"entry":[]}}]}}"
17:33:50.629 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 400 Bad Request
17:33:50.629 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Accept-Ranges: bytes
```
2 ,如果换成 resttemplate ,那么直接连日志也没有了。。response 和上述一样
我将日志等级修改 debug ,也是没有日志,只有 response 。
问题:如开头

debug, 日志, main

lsk569937453   
http 状态码 400 很明显了,你的请求入参不对。
RedBeanIce
OP
  
@lsk569937453
请再看一下我的问题。我们问题不是 400 。
而是具体的详细的报错,仅仅体现在日志中,无法让 ta 体现在返参中。
hmmm000   
resttemplate 可以通过 catch HttpStatusCodeException ,用 getResponseBodyAsString 方法获取非 200 返回的响应体。httpclient 不清楚。
xiaokongwu   
“如果在 response 里面获取,只能获取到 bad request ,没有具体的某个字段的错误。”
贴一下获取代码吧
您需要登录后才可以回帖 登录 | 立即注册

返回顶部