This is the second day of my participation in Gwen Challenge

Problem reproduction

When debugging the interface, the GET request can access the back-end service normally, while the POST request always times out. After setting the AXIOS timeout time, an error message is reported (the information is as follows). I/O error during input, Socket connection timed out.

Error message

I/O error while reading input message; nested exception is org.apache.catalina.connector.ClientAbortException: java.net.SocketTimeoutException

Problem analysis

An Internet search failed to find the exact cause of the error. The first step is to find out whether the problem is on the client side or the server side. Use Postman and the front-end application to call the same interface, respectively. The results are as follows:

  • The request body of a request sent by postman is displayed.
  • A request sent through a front-end application does not have a request body.

The request body is streaming data. If it has been consumed in other places, it will not be consumed in subsequent applications. That is, where the request body is consumed, considering that mock-serve is configured in the project. It’s probably the mock-server.

Check the mock-server configuration with the following code:

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
    extended: true
}))
Copy the code

Bodyparser. json is used to parse JSON data formats. Bodyparser. urlencoded is used to parse our usual form submission data, that is, the request header contains the following information: Content-Type: Application/X-www-form-urlencoded

There are four common content-type types:

  • Application/X-www-form-urlencoded Common form submissions
  • Multipart /form-data file submission
  • Application/JSON Submits data in JSON format
  • Text/XML Submits data in XML format

Bodyparser. urlencoded module is used to parse the data of req.body, and the original req.body is overwritten after successful parsing, or {} if parsing fails. This module has one attribute, extended, which is officially described as follows:

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). Defaults to true, but using the default has been deprecated.

The extended option allows configuration to parse data using QueryString (false) or QS (True). The default is true, but this is already deprecated.

Solutions:

Interface address configuration:

    proxy: {
      '/api': {
        target: 'http://localhost:18000'.changeOrigin: true.ws: true.secure: false.pathRewrite: {
          '^/api': '/'}}}before: require('./mock/mock-server.js')
Copy the code

Actually, I don’t understand why I mock when THE proxy is configured in my project.

Comment out the body configuration code in mock- Server. Request again, problem solved.

conclusion

If the interceptor is set up to consume the request body, the back end should make a copy of the request body and pass it down. The server is based on the microservices architecture. Port 18000 is gateway port, and 18002 is a concrete service port. Gateway services starting in debug mode also cause interfaces to request Cancled. As a Web developer, it is necessary to have a solid knowledge of HTTP.

Reference article:

www.jianshu.com/p/6d9a11f3c…

Github.com/eclipse/jet…