Ask a question about how to call Tencent Cloud API remotely by Restemplate.

recently learned to use Spring"s Restemplate for remote call API. The API document on Tencent"s side requires json format. There are three fields in the request header that need to be set: Host,Content-Type,Authorization (for more information, please see API document: https://cloud.tencent.com/doc., Request parameter data is available, encoded as follows

public R checkPhoto(Map<String,Object> param){
  // param
......
    String authtion = null;
    try {
            //  3600 
            authtion = SignUtil.appSign(appid,g.getSecretId(),g.getSecretKey(),Constant.TX_COS_BUCKET_AUTHINFO,3600);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("");
            return R.error("");
        }
        // 
        HttpHeaders requestHeader = new HttpHeaders();
        requestHeader.setContentType(MediaType.APPLICATION_JSON);
        requestHeader.add("Host","recognition.image.myqcloud.com");
        requestHeader.add("Authorization",authtion);
        // 
        Map<String,Object> paramMap = new HashedMap();
        paramMap.put("appid",appid);
        paramMap.put("urlA",urlA);
        paramMap.put("urlB",urlB);
        //  JSON postForObjectpost
        String str = JSON.toJSONString(paramMap);
        JSONObject json = JSON.parseObject(str);
        HttpEntity<JSONObject> request = new HttpEntity<JSONObject>(json,requestHeader);
        String response = this.restTemplate.postForObject(Constant.TX_FACE_URL_HTTP,request,String.class);
        .......

part of the code of Service is posted here. I would like to ask the gods why the request parameters of JSONObject will report an error of 400. Below, I use POJO to encapsulate the request parameters and get a successful call

.
public R checkPhoto(Map<String,Object> param){
  // param
......
    String authtion = null;
    try {
            //  3600 
            authtion = SignUtil.appSign(appid,g.getSecretId(),g.getSecretKey(),Constant.TX_COS_BUCKET_AUTHINFO,3600);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("");
            return R.error("");
        }
        // 
        HttpHeaders requestHeader = new HttpHeaders();
        requestHeader.setContentType(MediaType.APPLICATION_JSON);
        requestHeader.add("Host","recognition.image.myqcloud.com");
        requestHeader.add("Authorization",authtion);
        // *********************************************//
        // 
        TxAPIDto dto = TxAPIDto.builder().appid(g.getAppId()).urlA(urlA).urlB(urlB).build();
        HttpEntity<TxAPIDto> request = new HttpEntity(dto,requestHeader);
        ResponseEntity<TxAPIDto> apiresponse = this.restTemplate.exchange(Constant.TX_FACE_URL_HTTP, HttpMethod.POST,request,TxAPIDto.class);
        TxAPIDto respnose = apiresponse.getBody();
        .......
}

can be successfully called using DTO, but continue to call Tencent OCR identification API in this way (compared with the previous API parameter and return content is a bit more complicated. API: https://cloud.tencent.com/doc., Rivers and lakes emergency, kneel and thank God!

Mar.16,2021

personal guess :
may be because HttpEntity uses setter/getter to access the data when reading the data, that is to say, it does its own serialization operation, so JSONObject does not have these.


Why should I use JsonObject with DTO?


finally, it is found that it is the urination of Tencent interface, and the parameter value is a dynamic picture link. Just replace it with the latest one!

Menu