前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce Apex Rest Callout 更新JSON形式的数据

Salesforce Apex Rest Callout 更新JSON形式的数据

原创
作者头像
repick
发布2023-11-19 21:19:23
1480
发布2023-11-19 21:19:23
举报
文章被收录于专栏:Salesforce

callOutSample.cls

代码语言:javascript
复制
public with sharing class callOutSample {
    public static Map<String, Object> getInfoCallOut() {
        Map<String, Object> results = new Map<String, Object>();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        System.debug('>>>debuglog>>>>>>>>>>response>>>>>>>>>>>>>'+response);
        // If the request is successful, parse the JSON response.
        if(response.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        }
        return results;
    }
    public static Map<String, Object> setInfoCallOut(String inputJson) {
        Map<String, Object> results = new Map<String, Object>();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Set the body as a JSON object
        request.setBody(inputJson);
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if(response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
            results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        }
        return results;
    }
}

callOutApex.cls

代码语言:javascript
复制
public with sharing class callOutApex {
    public static void getInfoCallOut() {
        Map<String, Object> results = callOutSample.getInfoCallOut();
        // // Cast the values in the 'animals' key as a list
        List<Object> animals = (List<Object>) results.get('animals');
        System.debug('Received the following animals:');
        for(Object animal: animals) {
            System.debug(animal);
        }
    }
    public static void setInfoCallOut() {
        Map<String, Object> results = callOutSample.setInfoCallOut('{"name":"mighty moose"}');
        // // Cast the values in the 'animals' key as a list
        List<Object> animals = (List<Object>) results.get('animals');
        System.debug('Received the following animals:');
        for(Object animal: animals) {
            System.debug(animal);
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档