[SFMC] HTTP Request in SSJS

알린의 개발노트·2023년 6월 21일
0

예시 코드

Methods

  • setHeader(name, value) - 헤더 설정
  • removeHeader()
  • clearHeader()
  • send() - 요청 전송

Additional Properties

  • contentType - req.contentType = 'application/json';과 같이 사용
  • method - String value indicating HTTP method to use:
    • GET
    • DELETE
    • HEAD
    • OPTIONS
    • PATCH
    • POST
    • PUT
  • postData - 아래 예시처럼 사용
    var payload = {
      'name':'value'
    };
    req.postData = Stringify(payload);

POST

  <script runat=server>
    var req = new Script.Util.HttpRequest("http://www.example.com/");
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = "application/json"
    req.setHeader("headerTest","test header value");
    req.method = "POST";
    req.postData = "testing=testValue1";

    var resp = req.send();
    
    var resultStr = String(resp.content);
    var resultJSON = Platform.Function.ParseJSON(String(resp.content));

    return resultJSON;
  </script>

GET

  <script runat=server>
    var req = new Script.Util.HttpRequest(url);
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = 'application/json';
    req.method = 'GET';
    req.setHeader('Authorization', 'Bearer ' + token);

    var resp = req.send();
    
    var resultStr = String(resp.content);
    var resultJSON = Platform.Function.ParseJSON(String(resp.content));
  </script>

참고 자료

Script.Util.HttpRequest

profile
안녕하세요!

0개의 댓글