0

I am trying to do a PUT request. I am able to generate a token (using POST method) and am able to run this in POSTMAN and do a cURL command in the command line. I can't do it in SFMC though. I have tried the payload as JSON as well as this string in the code below. Any suggestions?

Here is my code:

Platform.Load("Core", "1.1.1");

try {
    var authHeader = "Bearer xxxxxx";
    var url = "https://eeeee.api.commercecloud.salesforce.com/pricing/coupons/v1/organizations/zxzxzxzxzxz/coupons/johnTestId1?siteId=TEST";
    var contentType = "application/json";
    var payload = "{\"couponId\":\"johnTestId1\",\"creationDate\":\"2024-07-25T04:56:35.929Z\",\"description\":\"Thiscouponisusedtogive10%offstuff.\",\"enabled\":false,\"exportedCodeCount\":0,\"lastModified\":\"2024-07-25T04:56:35.929Z\",\"redemptionCount\":3,\"redemptionLimits\":{\"limitPerCode\":1,\"limitPerCustomer\":1,\"limitPerTimeFrame\":{\"limit\":1,\"redemptionTimeFrame\":1}},\"systemCodesConfig\":{\"codePrefix\":\"TT\",\"numberOfCodes\":10},\"totalCodesCount\":10,\"type\":\"system_codes\"}";
    
    var req = new Script.Util.HttpRequest(url);
    req.method = "PUT";
    req.contentType = contentType;
    req.setHeader("Authorization", authHeader);
    req.postData = String(payload);

    var resp = req.send();
  
    //Write("Response Status Code: " + resp.statusCode + "<br>");
  
    //var payload = Platform.Function.ParseJSON(respContent);
    
    //var accessToken = payload.access_token;
  
    //Write(accessToken);

} catch (e) {
    Write("Exception: " + e);
}
</script>
3
  • Hi, there is no WSProxy in your code (developer.salesforce.com/docs/marketing/marketing-cloud/guide/…), you are using plain SSJS. We need to see your result to understand whats happening; My hunch is that you are escaping quotes in your payload AND stringifying it. There is a very good breakdown on your approach here gortonington.com/restapimethodinssjs Commented Jul 25 at 6:51
  • Hello @JonasLamberty - here is error I am getting from the catch: An error occurred when attempting to evaluate a Util.HttpRequest call. See inner exception for details. URL:https://eeeee.api.commercecloud.salesforce.com/pricing/coupons/v1/organizations/zxzxzxzxzxz/coupons/johnTestId1?siteId=TEST Method: PUT ClientID: 534002312 JobID: 0 ListID: 0 BatchID: 0 SubscriberID: 0
    – Emailer
    Commented Jul 25 at 13:19
  • Also @JonasLamberty - with the code below from August, I am catching this erorr now: Error in HTTP request: Matching constructor not found for: Script.Util.HttpRequest
    – Emailer
    Commented Jul 25 at 13:28

1 Answer 1

0

It is correct to use Script.Util.HttpRequest() to do PUT requests in SSJS. Below is a PUT function I have used to bulk upsert to a data extension.

var token = "xxxxx";
var restBaseURI = "https://xxxxxxx.rest.marketingcloudapis.com/";
var subArr = [ payload ]; //array
var externalkey = xxxx; //data extension key

var result = httprequest(token, restBaseURI, subArr, externalkey);

function httprequest(token, restBaseURI, subArr, externalkey) {
    var config = {
      URL : restBaseURI + "data/v1/async/dataextensions/key:" + externalkey + "/rows", 
      contentType : "application/json; charset=utf-8",
      headerName : "Authorization",
      headerValue : "Bearer " + token,
      method: "PUT",
      payload : {
       "items": subArr
       }
    }

    try {
    var req = new Script.Util.HttpRequest(config.url);
    var resp = req.send();

    if (resp.statusCode == 202 && resp.returnStatus == 0) {
       return true 
       }
       else { 
       return false
       }
    } 
    catch (e) {
    Write('"Error in HTTP request: ' + e.message);
    }
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .