2

I have the following extremely simple SpringBoot program

// APIEndpoints.java

// Imports!

public class APIEndpoints {
    @PostMapping("deduplicate")
    public String deduplicate(@RequestParam(value = "data") String data) {
        return data;
    }
}
// RestServiceApplication.java

@SpringBootApplication
public class RestServiceApplication {

    public static void main(String[] args) throws SQLException {
        SpringApplication.run(RestServiceApplication.class, args);
    }
}

I can start the springboot server via ./gradlew bootRun, and have verified that the server is working through other endpoints.

Here is my issue: using postman to send a post request, the following goes off without a hitch

localhost:8080/deduplicate?data=1,23,4,5

However, this one fails with an error: "HTTP 400: Bad Request"

localhost:8080/deduplicate?data=[1,23,4,5]

This seems like undesirable behavior, and it doesn't seem to be a fundamental limitation of url formatting or anything like that.

What is causing this error, and how can I set up Spring Boot to accept lists enclosed in brackets?

pipsqueaker117
  • 2,078
  • 8
  • 29
  • 43

3 Answers3

2

So the main reason for getting this error is the characters "[" and "]". More details about URLs and URIs allowed characters in this answer: Which characters make a URL invalid?.

The best way (following the standard) - encode URL on client side:

encodeURL("localhost:8080/deduplicate?data=[1,23,4,5]")
>localhost:8080/deduplicate?data=%5B1%2C23%2C4%2C5%5D

Using postman, you want to select your data in query params window, click right mouse button and select "EncodeURIComponent":

Example of encoding using postman will transfer your URL to

 localhost:8080/deduplicate?data=%5B1%2C23%2C4%2C5%5D

And it can be successfully read by tomcat (i suppose you use it as servlet container).

If you can not change your front-end behavior, you can use

relaxedQueryChars / relaxedPathChars

in the connectors definition to allow these chars. Using java and spring (if tomcat is embedded):

    @Component
    public class TomcatWebServerSettings implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
        @Override
        public void customize(TomcatServletWebServerFactory factory) {
            factory.addConnectorCustomizers(connector ->
            {
                connector.setAttribute("relaxedQueryChars", "[]");
            });
        }
    }

Or you can add relaxedQueryChars attribute under server.xml (%TOMCAT_FOLDER%/conf/):

  <Connector 
        //other params,
          relaxedQueryChars="[,]"
         />

Also, you can downgrade your tomcat to version under 7.0.76 (strongly not recommended - security reasons).

Nikita
  • 36
  • 3
0

either

deduplicate?data=1,23,4,5

or

deduplicate?data=1&data=23&data=4&data=5

thats how it work if you want to as an array via REST API as request param.

domingo
  • 147
  • 5
0

You are using unsafe characters in url, therefore you have troubles. RFC1738

" < > # % { } | \ ^ ~ [ ] ` including the blank/empty space.
Ihar Sadounikau
  • 677
  • 6
  • 17