apache / shenyu

Apache ShenYu is a Java native API Gateway for service proxy, protocol conversion and API governance.
https://shenyu.apache.org/
Apache License 2.0
8.44k stars 2.93k forks source link

If the content-type is set "application/x-www-form-urlencoded",and method is "GET", RpcParamTransformPlugin can't get body from HttpRequest. #2731

Closed stanxlab closed 2 years ago

stanxlab commented 2 years ago

Is there an existing issue for this?

Current Behavior

The GET request header set content-type: application/x-www-form-urlencoded, RpcParamTransformPlugin will get body from HttpRequest, but in GET requset the body is empty, the parameters is in url query part.

The code is in the RpcParamTransformPlugin.java

     if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) {
             return formData(exchange, request, chain);
     }

...

    private Mono<Void> formData(final ServerWebExchange exchange, final ServerHttpRequest serverHttpRequest, final ShenyuPluginChain chain) {
        return Mono.from(serverHttpRequest.getBody()
                .flatMap(map -> {
                    String param = resolveBodyFromRequest(map);
                    LinkedMultiValueMap linkedMultiValueMap;
                    try {
                        linkedMultiValueMap = BodyParamUtils.buildBodyParams(URLDecoder.decode(param, StandardCharsets.UTF_8.name()));
                    } catch (UnsupportedEncodingException e) {
                        return Flux.error(e);
                    }
                    exchange.getAttributes().put(Constants.PARAM_TRANSFORM, HttpParamConverter.toMap(() -> linkedMultiValueMap));
                    return chain.execute(exchange);
                }));
    }

Expected Behavior

The GET request header set content-type: application/x-www-form-urlencoded, RpcParamTransformPlugin can get parameters from url query.

Steps To Reproduce

No response

Environment

ShenYu version(s): 2.4.1

Debug logs

No response

Anything else?

No response

yu199195 commented 2 years ago

why get request,set content-type "application/x-www-form-urlencoded " ?

stanxlab commented 2 years ago

@yu199195 I'm sorry. It's my fault. The header is set content-type "application/x-www-form-urlencoded" both get and post request.