Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

rest - Search request with array parameters using HTTP Get

So, the problem I'm having is that in some cases my application needs a more complex search and filtering, and because of that, the requests need some parameters. In sometimes is necessary to pass an array with many entries which can cause the overflow of the URI query parameter limitation.

Since HTTP Get requests shouldn't have an HTTP body, the easiest solution to solve this was to change the request method to Post. But I find this a problem and was expecting that someone could show me better solutions to this problem that I was just too inexperienced to see.

From this

HTTP Get /resource?array=[''...] //could reach the maxlenght of the query parameters

to this

HTTP Post /resource/search
body: {
  'array': ['' ...]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since HTTP Get requests shouldn't have an HTTP body, the easiest solution to solve this was to change the request method to Post. But I find this a problem and was expecting that someone could show me better solutions to this problem that I was just too inexperienced to see.

As of 2020-12-30, there isn't a best solution.

Your choices are

  • GET, and run into the defacto limits on target-uri length
  • POST, and lose both caching and the benefits of safe semantics
  • YOUR_BEPOKE_METHOD_TOKEN_HERE, and lose generality (general purpose components won't know the semantics of your bespoke method)
  • NEW_STANDARDIZED_METHOD_TOKEN, which requires going through the standardization process and then driving adoption.

The good news, is that as of 2020-10, the HTTP working group has adopted a proposal to address this (in other words, other people are working on the NEW_STANDARDIZED_METHOD_TOKEN approach).

My guess is that the working group will eventually produce a specification that relaxes the restrictions on the existing SEARCH method, so that it can be used for payloads beyond those defined in the WebDAV specification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...