Record Ajax requests report 415 and 404 problems

记得要微笑
中文

Problem description and analysis

Today, I helped a colleague next to my classmate to solve a problem. The problem is this: We have a project agentBuy ( Freemark+JQuery ) that is not separated from the front and back ends. The colleague wants to start the agentBuy service ( http:localhost:8001 ) locally, and web-inquiry the local 06175129176091 service of the http://127.168.24.68:9366 ), direct joint debugging will have cross-domain problems, so my colleague started the gateway service ( spring cloud gateway ) web-inquiry agentBuy and 06175129176098 services to http:webagent.java.com:10000 for joint debugging, but found that the interface reported 415 .

// agentBuy  
var params = {
  "storeId":"HL000001",
  "quoteType":"AUTO",
  "enable":"Y"
};
$.ajax({
  url: WEB_ROOT + '/inquiryWeb/supply/quote/enable', // 在本地为http:webagent.java.com:10000/inquiryWeb/supply/quote/enable
  type: 'post',
  data : params,
  dataType: 'json',
  success: function (response) {
    // code...
  }
})

It is found that the requested participation in the conference is transformed into the form of key-value pairs, Request Payload :

storeId=HL000001&quoteType=AUTO&enable=Y

The response status code is 415 ( Unsupported Media Type ), indicating that the server cannot process the media format attached to the request (unsupported media type)

@Log4j2
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SupplyQuotationConfigController {

  private final SupplyQuotationConfigClient supplyQuotationConfigClient;

  private static final String REQUEST_NOT_VALIDATE = "请求参数校验不通过~";
  private static final String INTERNAL_SERVER_ERROR = "网络异常,请稍后重试~";

  @PostMapping("/supply/quote/enable")
  public ResponseEntity<Result<Boolean>> setSupplyQuoteEnable(@RequestBody SupplyQuoteEnableRequest request) {
    if (null == request || !request.validate()){
      return new ResponseEntity<>(new Result<>(HttpStatus.BAD_REQUEST.value(), REQUEST_NOT_VALIDATE, Boolean.FALSE), HttpStatus.OK);
    }
    try {
      supplyQuotationConfigClient.setSupplyQuoteEnable(SupplyQuotationConfigFactory.toSupplyQuotationEnableRequest(request));
      return new ResponseEntity<>(new Result<>(HttpStatus.OK.value(), null, Boolean.TRUE), HttpStatus.OK);
    }catch (HttpMessageException e){
      return new ResponseEntity<>(new Result<>(e.getStatusCode(), e.getMessage(), Boolean.FALSE), HttpStatus.OK);
    }catch (Exception e){
      return new ResponseEntity<>(new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), INTERNAL_SERVER_ERROR, Boolean.FALSE), HttpStatus.OK);
    }
  }
}

After investigating the back-end interface, it is found that the request parameter uses the @RequestBody json string data passed by the front end to the back end. The type of the key-value pair is now passed, because if you do not specify request header Content-Type , the default is application/x-www-form-urlencoded , so before and after The parameter types defined by the client are inconsistent, and the error 415

In request header set Content-Type:application/json can solve 415 problem, but re-request has reported 404 , I carefully control again request path, and I found no problems, and the use of postman local debugging interface is not a problem, puzzling .

Later, it was found that request header was set in Content-Type:application/json , but Request Payload is still a key-value pair:

storeId=HL000001&quoteType=AUTO&enable=Y

Is it because the key-value pair parameter will be appended to URL , making the request path wrong? Why does this become a key-value pair?

Because when there is no MIME type, or when some browsers think that the set MIME type is incorrect, the browser may perform MIME MIME type by looking at the bytes of the resource. Above we set Content-Type:application/json , application/json means that we need to pass a json string, but we are passing json data, the browser thinks that the MIME set is incorrect, and the parameter is judged as a key-value pair form, which leads to subsequent requests to report 404 .

We only need to change the json parameter to 0617512917640a string to solve this problem:

// agentBuy  
var params = {
  "storeId":"HL000001",
  "quoteType":"AUTO",
  "enable":"Y"
};
$.ajax({
  url: WEB_ROOT + '/inquiryWeb/supply/quote/enable', // 在本地为http:webagent.java.com:10000/inquiryWeb/supply/quote/enable
  type: 'post',
  data : JSON.stringify(params), // 改成json字符串
  dataType: 'json',
  success: function (response) {
    // code...
  }
})

refer to:

Ajax post request error 415 or 400 solution

MIME sniffing

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Type

阅读 709
avatar
记得要微笑
前端工程师

求上而得中,求中而得下,求下而不得

1.2k 声望
3k 粉丝
0 条评论
avatar
记得要微笑
前端工程师

求上而得中,求中而得下,求下而不得

1.2k 声望
3k 粉丝
文章目录
宣传栏