티스토리 뷰

1.     Authorize Endpoint Address

-       외부 사이트에서 콜아웃을 할 때, 원격 사이트 설정 페이지에 해당 페이지가 등록되어 있어야 한다.

-       Setup -> Remote Site Settings -> new에서 원격 사이트를 등록해야 한다.

 

 

2.     HTTP 및 콜아웃

-       REST 콜아웃은 HTTP 기반으로 이루어진다.

-       각 콜아웃 요청은 HTTP 메소드 및 엔드 포인트와 연결된다.

-       가장 간단한 요청은 HTTP 메소드인 GET 요청이다.

-       GET 요청은 보낸 사람이 서버에서 리소스에 대한 정보를 얻고자 하는 것이다.

-       서버가 GET 요청을 수신하고 처리하면 요청정보가 수신자에게 반환된다.

 

 

3.     HTTP의 메소드 종류

-       1) GET

-        : URL로 식별된 데이터를 검색한다.

-       2) POST

-        : 리소스를 생성하거나 서버에 데이터를 게시한다.

-       3) DELETE

-        : URL로 식별되는 리소스를 삭제한다.

-       4) PUT

-        : 요청 본문에서 보낸 리소스를 만들거나 교체한다.

 

 

4.     실습 과제

 

 

 

5.     실습 코드

 

public class AnimalLocator {
    
    public static String getAnimalNameById (Integer num) {
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + num);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        Map<String, Object> animals = (Map<String, Object>) results.get('animal');
        String animalsName = (String) animals.get('name');
        
        return animalsName;
        
    } // getAnimalNameById

} // end class

 

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HTTPResponse respond(HTTPRequest request) {
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal":{"id":2,"name":"bear","eats":"berries, campers, adam seligman","says":"yum yum"}}');
        response.setStatusCode(200);
        return response; 
    } // respond

} // end class

 

@isTest
global class AnimalLocatorTest {
    
    @isTest 
    static void testPostCallout() {
        
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock()); 

        String response = AnimalLocator.getAnimalNameById(2);
        
        System.debug(response);
        
    } // testPostCallout

} // end class

 

 

 

728x90

'[세일즈포스 개발자]' 카테고리의 다른 글

Debug Log ( 디버그 로그 )  (0) 2023.03.02
WSDL 및 APEX SOAP Callouts  (0) 2023.02.24
Org Development Model  (0) 2023.02.23
Apex Testing  (0) 2023.02.23
Command-Line Interface ( 명령줄 인터페이스 )  (0) 2023.02.23
댓글
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
최근에 올라온 글
Total
Today
Yesterday
공지사항