scenario

After interface docking with the supplier, I conducted debugging in Postman and found that it could be called normally, but the other party reported wrong request mode during the joint debugging, which took a lot of time.

  • Request type

    POST

  • The requested url https://xxxxx/vehicleOperate/openapi/gateMachine/inRecord/518021_0054/parkcarin

  • Request parameters

    {" carinlist: [{" id ":" 197 ", "entrance", "entrance", "cardId" : "BDF2413131313131", "cardNo" : "tianjin A11111", "carNo" : CardTypeId: 31" cardTypeName: Temporary card A", entranceTime: "2017-06-14 08:38:29", "entranceWayId": "0", "entranceWayName": "normal entry ", "small": "0", "entryPic": "", "MachNo":"1" }]", "signature":"SJDKLSJKLDJSKLSHJKSHDJKHJKHJK", "t":"1520416876" }Copy the code

    Interface code

    ``` @PostMapping("/inRecord/{parkId}/parkcarin") public DoorRecordResult createInRecord(@RequestBody DoorGateMachineRecordInfoInVO doorGateMachineRecordInfoVO, @PathVariable("parkId") String parkId) { DoorRecordResult doorRecordResult = new DoorRecordResult(); SSOUser ssoUser = getCurrentUser(); The info (" parkId: {}, admission information consumption: {} ", parkId, JSONObject. ToJSONString (doorGateMachineRecordInfoVO)); List<DoorGateMachineRecordInVO> carinlist = doorGateMachineRecordInfoVO.getCarinlist(); carinlist.stream().forEach(g -> { g.setCreatorId(ssoUser.getId()); g.setCreatorName(ssoUser.getUsername() + "/" + ssoUser.getRealname()); g.setCreateTime(new Date()); }); doorRecordResult = gateMachineService.handleInRecord(carinlist, parkId); Log.info (" parkId: {}, result: {}", parkId, jsonObject.tojsonString (doorRecordResult)); return doorRecordResult; } ` ` `Copy the code

    The postman call interface can be used to call the returned data, but it is not possible to call the other party’s application. We tried several ways but failed, and finally found that one item was missing. Content-type is application/ X-www-form-urlencoded, the content-type is application/ X-www-form-urlencoded, The content-Type supported by my interface is actually Application/JSON, so the interface cannot be called. The content-Type must be specified in the interface document, otherwise the interface development will be seriously affected.

    What is the difference between Application/X-www-form-urlencoded and Application /json? The first thing to understand is that there are several content-Types

  1. Application/X-www-form-urlencoded Application/X-www-form-urlencoded application is the most common way of Posting data. Browser native

    Forms, if encType is not set, will eventually submit data as Application/X-www-form-urlencoded, and receive data of this type in springMVC via @requestParam, such as the interface above can be modified to

    @PostMapping(value = "/inRecord/{parkId}/parkcarin",consumes = "application/x-www-form-urlencoded")
    public DoorRecordResult createInRecord(@RequestParam (value = "carinlist") String carinlistStr,
                                           @PathVariable("parkId") String parkId) {
    
        -----
        carinlistStr = parameterMap.get("carinlist")[0];
    
    }
    Copy the code

    You can also use springMVC’s annotations directly from HttpServerlet, for example

    @PostMapping(value = "/inRecord/{parkId}/parkcarin",consumes = "application/x-www-form-urlencoded") public DoorRecordResult createInRecord(HttpServletRequest request, @PathVariable("parkId") String parkId) { List<DoorGateMachineRecordInVO> carinlist = null; Map<String, String[]> parameterMap = request.getParameterMap(); try { String carinlistStr = parameterMap.get("carinlist")[0]; Log.info (" Entry message consumption processing started parkId: {}, received data: {}", parkId,carinlistStr); carinlist = JacksonUtil.toList(carinlistStr, DoorGateMachineRecordInVO.class);Copy the code

    @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody: @requestBody

  2. application/json

    You’re probably familiar with content-Type as a response header. In fact, it is increasingly used as a request header to tell the server that the message body is a serialized JSON string. Due to the popularity of the JSON specification, json.stringify is natively supported in every major browser except for earlier versions of IE, and all server-side languages have functions that handle JSON without any trouble. This type of response header is very common nowadays, and the parameter that receives it is @requestBody. The reason for the interface failure is that the response header I used here is actually application/ JSON

  3. multipart/form-data

    This is another common way to POST data. When we use forms to upload files, we must have the encType of the form equal to multipart/form-data

  4. text/xml

    It is a remote invocation specification that uses HTTP as the transport protocol and XML as the encoding method. A typical XML-RPC request looks like this:

    POST www.example.com HTTP/1.1 Content-type: text/ XML examples.getStateName 41

In addition to the Content-Type, note how application/ X-www-form-urlencoded requests are tested on Postman. Since the incoming parameters are complex arrays, consider how Postman passes arrays.

At first I thought it was going like this

It didn’t work until I found out it was

It can be seen that the default postman line corresponds to a key-value. At that time, because I was not familiar with the call of Postman, I used the call form through Java code, and the two methods were similar. With that in mind, I also learned a little bit about HTTP request headers

Accept: The content types accepted by the client text/plain, text/ HTML

User-agent: indicates the information about the sender of the request

conclusion

Http interface is the most simple interface in many interfaces, but if you are not familiar with his request, the request body, and the corresponding framework of receiving treatment, also can produce a series of problems, only to discover the problems in the practice, to the next processing interface to make the development of better processing functions, faster processing related issues, improve the efficiency of development.