0

I have a swagger spec

/authentication-service/authorize:
  post:
    tags:
      - Auth
    summary: Authorize
    requestBody:
      content:
        application/x-www-form-urlencoded:
          schema:
            properties:
              redirect_uri:
                type: string
                example: http://localhost
              client_id:
                type: string
                example: asdf
          encoding:
            redirect_uri:
              allowReserved: true
            client_id:
              allowReserved: true
    parameters:
      - name: Content-Type
        in: header
        schema:
          type: string
        example: application/x-www-form-urlencoded
    responses:
      '200':
        description: Successful response
        content:
          application/json: {}

as you can see it is a post with content type of x-www-form-urlencoded with a requestBody.

This is what org.openapitools:openapi-generator-maven-plugin:6.0.1 generates out of that:

    @Operation(
        operationId = "authenticationServiceAuthorizeGet",
        summary = "API-AS02 Authorize",
        tags = { "Auth" },
        responses = {
            @ApiResponse(responseCode = "200", description = "Successful response")
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/authentication-service/authorize",
        produces = "application/json",
        consumes = "application/x-www-form-urlencoded"
    )
    ResponseEntity<Void> authenticationServiceAuthorizePost(
        @Parameter(name = "Content-Type", description = "") @RequestHeader(value = "Content-Type", required = false) String contentType,
        @Parameter(name = "redirect_uri", description = "") @Valid @RequestParam(value = "redirect_uri", required = false) String redirectUri,
        @Parameter(name = "client_id", description = "") @Valid @RequestParam(value = "client_id", required = false) String clientId,
 
...more params generated...
    );

Now when I finally try to use this feign client, it compiles following request for me:

[POST] to [https://api-us-dev<obfuscatedURL>/authentication-service/authorize?redirect_uri=http%3A//localhost&client_id=oidc_pkce....

So as you can see the compiled feign client calls POST request with encoded query params that were delcared as requestBody in the spec above.

This request is being rejected from the server. The server is avaiting those params to be encoded in the body of the request.

However that's how openapi generator & feign client does it for me.

2 questions on that:

  • Is it generated correctly?
  • Regardless of it being correct or not, I need to send payload in the body as per server impl. Can you point out to me how to change swagged spec for it to be encoded as body k=v pairs?

How to POST form-url-encoded data with Spring Cloud Feign

does not do it for me, the parameters are still encoded in the query instead of body

greengold
  • 1,184
  • 3
  • 18
  • 43
  • Does this answer your question? [How to POST form-url-encoded data with Spring Cloud Feign](https://stackoverflow.com/questions/35803093/how-to-post-form-url-encoded-data-with-spring-cloud-feign) – Andrey B. Panfilov Oct 03 '22 at 08:43
  • will try that out, i suppose you're pointing out the formDataEncoder to me.. – greengold Oct 03 '22 at 08:48
  • i think that encoder is a good point, but I need to make it generate `@requestBody` instead of `@RequestParam` because with @RequestParam it keeps sending it as query params even with that encoder – greengold Oct 03 '22 at 09:11
  • how do you call it? https://github.com/OpenFeign/feign-form/blob/master/feign-form/src/main/java/feign/form/FormEncoder.java#L89 expects that `Content-Type` is set to `application/x-www-form-urlencoded` – Andrey B. Panfilov Oct 03 '22 at 09:19
  • it is set. i can tell because i have an interceptor and i see it in the headers. also generated code has `consumes = "application/x-www-form-urlencoded"` in it – greengold Oct 03 '22 at 09:22
  • just have checked and that call in not hitting the encoder you pointed out. that might be a problem too... i think problem is in my swagger markup, maybe it's not explicit enough for openapi to generate it the right way, idk – greengold Oct 03 '22 at 09:43

0 Answers0