question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

curl post application/x-www-form-urlencoded throw MissingServletRequestParameterException

See original GitHub issue

I use spring-boot 2.2.x

My code is:

   @PostMapping(path = "/testFormData", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  public BaseResult testFormData(@RequestParam(name = "name") String name,
      @RequestParam(name = "age") Integer age) {
    log.info("api testFormData name is {}, age is {}", name, age);
    return BaseResult.success(name + "---" + age);
  }

and I use curl:

 curl -XPOST "http://127.0.0.1:8082/api/testFormData"  -d "name=jackson&age=24"

and it throws a MissingServletRequestParameterException.

If I use spring-boot 2.1.x, the request is OK.

How can I resolve application/x-www-form-urlencoded post request?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dragontree101commented, May 14, 2020

i found i add an accessLogFilter, and create a HttpServletRequestAdapter

if i add

requestWrapper = new HttpServletRequestAdapter(request, bytes);

param has this problem

if i am not use

HttpServletRequestAdapter

every thing is ok

 @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
      FilterChain filterChain) throws ServletException, IOException {
    HttpServletRequestAdapter requestWrapper = null;
    ContentCachingResponseWrapper responseWrapper = null;
    byte[] bytes = IOUtils.toByteArray(request.getInputStream());

    requestWrapper = new HttpServletRequestAdapter(request, bytes);
    filterChain.doFilter(request, response);
  }

public class HttpServletRequestAdapter extends HttpServletRequestWrapper {

    private InputStream inputStream;

    public HttpServletRequestAdapter(HttpServletRequest request, byte[] payload) {
      super(request);
      inputStream = new ByteArrayInputStream(payload);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
      return new ServletInputStream() {
        @Override
        public boolean isFinished() {
          return false;
        }

        @Override
        public boolean isReady() {
          return false;
        }

        @Override
        public void setReadListener(ReadListener listener) {

        }

        @Override
        public int read() throws IOException {
          return inputStream.read();
        }
      };
    }
  }

but i use spring-boot 2.1.x also add this accessLogFilter, request param is ok.

1reaction
mpreziusocommented, Jun 23, 2022

We were having the same problem and we later realised it was because of a filter in the chain that was consuming the request InputStream.

If you are doing the same, ContentCachingRequestWrapper and ContentCachingResponseWrapper may be useful as well as relying on AbstractRequestLoggingFilter if you’re doing that for logging purposes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Content type 'application/x-www-form-urlencoded;charset ...
The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use this we...
Read more >
How do I post form data using Curl? - ReqBin
In this Curl POST Form example, we submit a form to a ReqBin echo URL in the application/x-www-form-urlencoded format. Click Run to execute ......
Read more >
How do I make a POST request using curl? - Super User
This will be POST-ed as application/x-www-form-urlencoded (used for the majority of forms that don't contain file uploads): curl http://httpbin.org/post ...
Read more >
How to send POST Request with JSON Payload using Curl ...
In this article, you will learn how to post JSON data with curl command to test your Spring REST application. If you know...
Read more >
Perform a POST Request Using Curl [Practical Examples]
application /x-www-form-urlencoded which is used when submitting a web form to the server and multipart/form-data which is used to upload files to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found