개발/spring
[webClient] 301 move permanently
방푸린
2023. 1. 3. 10:26
반응형
GET api를 브라우저나 포스트맨으로 api를 쐈을 때는 잘 나오는데, webClient나 curl로 쏘면 아래와 같이 안 나오는 경우가 있다.
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
테스트해 보니 url에 https:// 를 안 붙이면 나는 듯하다.
보통은 이렇게 해결하면 되지만 애초에 ssl 설정이 안 되어 있는 곳이 있다.
추가적인 옵션를 주는 방법이 있지 않을까 해서 살펴본다.
curl은 -L 옵션을 주면 아래와 같이 자동 redirect를 해서 결과가 나오는 것을 볼 수 있다.
-L, --location
(HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
If used together with -i, --include or -I, --head, headers from all requested pages will be shown. When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to intercept the user+password. See also --location-trusted on how to change this. You can limit the amount of redirects to follow by using the --max-redirs option.
When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.
curl -L -v 'dev.com/api/external/money-exchange/configuration?exchangeGameType=&sync=false'
* Trying 10....
* TCP_NODELAY set
* Connected to dev.com (10..) port 80 (#0)
> GET /api/external/money-exchange/configuration?exchangeGameType=&sync=false HTTP/1.1
> Host: dev.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.18.0
< Date: Tue, 03 Jan 2023 01:05:17 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: https://dev.com/api/external/money-exchange/configuration?exchangeGameType=&sync=false
<
* Ignoring the response-body
* Connection #0 to host dev.com left intact
* Issue another request to this URL: 'https://dev.com/api/external/money-exchange/configuration?exchangeGameType=&sync=false'
* Trying 10...
* TCP_NODELAY set
* Connected to dev.com (10.) port 443 (#1)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=KR; ST=Gyeonggi-do; O=N Corporation; CN=*..com
* start date: Dec 21 00:00:00 2022 GMT
* expire date: Jan 21 23:59:59 2024 GMT
* subjectAltName: host "dev.com" matched cert's "*..com"
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=Sectigo Limited; CN=Sectigo RSA Organization Validation Secure Server CA
* SSL certificate verify ok.
> GET /api/external/money-exchange/configuration?exchangeGameType=&sync=false HTTP/1.1
> Host: dev.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200
< Server: nginx/1.18.0
< Date: Tue, 03 Jan 2023 01:05:17 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Connection #1 to host dev.com left intact
{"header":{"status":200,"message":"OK","isSuccessful":true},"result":{}}}
* Closing connection 0
* Closing connection 1
curl도 -L 옵션을 주면 되니, webclient도 관련 옵션이 있을 것 같아 찾아보았는데..
this.webClient = webClientBuilder.clone()
.baseUrl(url)
.defaultHeaders(httpHeaders -> httpHeaders.set(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.clientConnector(new ReactorClientHttpConnector( //
HttpClient.create().followRedirect(true) ////
)) //
.build();
위와 같이 redirect true 옵션을 주면 된다.
https://stackoverflow.com/questions/47655789/how-to-make-reactive-webclient-follow-3xx-redirects
728x90
반응형