401:“{“exception_body”:{“http_response_code”:401,“message”:“不存在会话。”,“error_code”:1000}}”

问题描述 投票:0回答:0

我在 Springboot 项目中使用 RestTemplate 进行 PATCH api 调用。下面是我使用的代码。

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("X-Requested-By",sessionID);
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
           
    restTemplate.setRequestFactory(requestFactory);
    String json = "json payload.."

    HttpEntity<String> request = new HttpEntity<String>(json, headers);
    String result = restTemplate.patchForObject(uri,request ,String.class);

我设置从邮递员会话 api 响应中获取的

sessionID
。当我为相同的 PATCH 请求在邮递员中将相同的 sessionID 设置为
X-Requested-By
标头时,它起作用了。但不适用于上面的代码。它给出以下 401 错误。

[           main] o.a.http.impl.execchain.MainClientExec   : Executing request PATCH /otmmapi/v6/assets HTTP/1.1
    2023-03-03 06:44:41.338 DEBUG 8224 --- [           main] o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED
    2023-03-03 06:44:41.339 DEBUG 8224 --- [           main] o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED
    2023-03-03 06:44:41.342 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> PATCH /otmmapi/v6/assets HTTP/1.1
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Accept: text/plain, application/json, application/*+json, */*
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Content-Type: application/json
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> X-Requested-By: 595627853
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Content-Length: 1328
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Host: url.com
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Connection: Keep-Alive
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.4 (java 1.5)
    2023-03-03 06:44:41.343 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 >> Accept-Encoding: gzip,deflate
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "PATCH /otmmapi/v6/assets HTTP/1.1[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Content-Type: application/json[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "X-Requested-By: 595627853[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Content-Length: 1328[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Host: url.com[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.4 (java 1.5)[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "[\r][\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "{[\n]"
    2023-03-03 06:44:41.344 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 >> "        "json payload"
    2023-03-03 06:44:41.728 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "HTTP/1.1 401 [\r][\n]"
    2023-03-03 06:44:41.728 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Date: Fri, 03 Mar 2023 01:14:43 GMT[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Content-Type: application/json[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Content-Length: 110[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Connection: keep-alive[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Set-Cookie: AWSALB=DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGrKMMutBCWy8RukeWj9PgeLM06; Expires=Fri, 10 Mar 2023 01:14:43 GMT; Path=/[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Set-Cookie: AWSALBCORS=DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGrKMMutBCWy8RukeWj9PgeLM06; Expires=Fri, 10 Mar 2023 01:14:43 GMT; Path=/; SameSite=None; Secure[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Server: Apache/2.4.52 (Unix)[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Cache-Control: no-store[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "X-Content-Type-Options: nosniff[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Pragma: no-cache[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Vary: Accept,Accept-Encoding[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Content-Encoding: gzip[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "Set-Cookie: BIGipServerpool_QA_creativestudio-qa.sysco.com_443=3786966282.47873.0000; path=/; Httponly; Secure[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"
    2023-03-03 06:44:41.729 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "[0x1f][0x8b][0x8][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x85][0x8c]A[\n]"
    2023-03-03 06:44:41.730 DEBUG 8224 --- [           main] org.apache.http.wire                     : http-outgoing-0 << "[0x80] [0x14][0x5][0xaf]"-[0xa1][0xd0][0xaa]Ct[0x5]I}[0x94][0x8b][0xfa][0xe2]70[0xa2][0xbb][0xe7][0xa2]}[0xcb][0x81][0x99][0xb9][0x9]- [0xd7][0xc4][0x87][0xf3][0x1c]/[0x9a]n[0xda]j[0xcd][0xae]@2[0x1f][0x2][0x17]8[0x82][0xa6][0xd1]XM;D[0x96][0xb5][0x13][0xcd][0xac][0xa4]C[0xaf][0x14]Z[0x92]*[0x3]i[0x8a][0xf0][0xe7][0xea]~$[0x94][0xc2][0xe5][0x9b]Zc[0xcc][0xf3][0xbc]:[0x92]6J[0x83][0x0][0x0][0x0]"
    2023-03-03 06:44:41.733 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << HTTP/1.1 401 
    2023-03-03 06:44:41.733 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Date: Fri, 03 Mar 2023 01:14:43 GMT
    2023-03-03 06:44:41.733 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Content-Type: application/json
    2023-03-03 06:44:41.733 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Content-Length: 110
    2023-03-03 06:44:41.733 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Connection: keep-alive
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Set-Cookie: AWSALB=DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGrKMMutBCWy8RukeWj9PgeLM06; Expires=Fri, 10 Mar 2023 01:14:43 GMT; Path=/
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Set-Cookie: AWSALBCORS=DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGrKMMutBCWy8RukeWj9PgeLM06; Expires=Fri, 10 Mar 2023 01:14:43 GMT; Path=/; SameSite=None; Secure
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Server: Apache/2.4.52 (Unix)
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Cache-Control: no-store
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << X-Content-Type-Options: nosniff
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Pragma: no-cache
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Vary: Accept,Accept-Encoding
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Content-Encoding: gzip
    2023-03-03 06:44:41.734 DEBUG 8224 --- [           main] org.apache.http.headers                  : http-outgoing-0 << Set-Cookie: BIGipServerpool_QA_creativestudio-qa.sysco.com_443=3786966282.47873.0000; path=/; Httponly; Secure
    2023-03-03 06:44:41.738 DEBUG 8224 --- [           main] o.a.http.impl.execchain.MainClientExec   : Connection can be kept alive indefinitely
    2023-03-03 06:44:41.740 DEBUG 8224 --- [           main] o.a.http.impl.auth.HttpAuthenticator     : Authentication required
    2023-03-03 06:44:41.740 DEBUG 8224 --- [           main] o.a.http.impl.auth.HttpAuthenticator     : url:443 requested authentication
    2023-03-03 06:44:41.740 DEBUG 8224 --- [           main] o.a.http.impl.auth.HttpAuthenticator     : Response contains no authentication challenges
    2023-03-03 06:44:41.757 DEBUG 8224 --- [           main] o.a.h.c.protocol.ResponseProcessCookies  : Cookie accepted [AWSALB="DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGr...", version:0, domain:url.com, path:/, expiry:Fri Mar 10 06:44:43 IST 2023]
    2023-03-03 06:44:41.758 DEBUG 8224 --- [           main] o.a.h.c.protocol.ResponseProcessCookies  : Cookie accepted [AWSALBCORS="DjJeq6qpFJCOtx/pi0jYMCpU0l4sh66CTVnj+O8xkHtZKnfJv1gb+Fp6OH728GRf9ZzF2ZV8kOhYNHjFvZ9Y4KaW30w/XxtA/zGr...", version:0, domain:url.com, path:/, expiry:Fri Mar 10 06:44:43 IST 2023]
    2023-03-03 06:44:41.758 DEBUG 8224 --- [           main] o.a.h.c.protocol.ResponseProcessCookies  : Cookie accepted [BIGipServerpool_QA_creativestudio-qa.sysco.com_443="3786966282.47873.0000", version:0, domain:creativestudio-qa.sysco.com, path:/, expiry:null]
    2023-03-03 06:44:41.762 DEBUG 8224 --- [           main] h.i.c.PoolingHttpClientConnectionManager : Connection [id: 0][route: {s}->https:url.com:443] can be kept alive indefinitely
    2023-03-03 06:44:41.762 DEBUG 8224 --- [           main] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 0][route: {s}->https://url.com:443][total kept alive: 1; route allocated: 1 of 5; total allocated: 1 of 10]
    Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{"exception_body":{"http_response_code":401,"message":"No session exists.","debug_message":"No session exists.","error_code":1000}}"

感谢快速回复。

java httpclient resttemplate patch http-status-code-401
© www.soinside.com 2019 - 2024. All rights reserved.