0

I am calling third party API via HTTPPOST2 using ampscript, and need response body for response codes like 4xx or 5xx. For success scenario, I am getting the response body in response parameter of HTTPPOST2 request. Please find below my code for the same:

<script language="javascript" runat="server"> 
    Platform.Load("Core","1");  
    try{ 
</script>
%%[

var @payload, @postRequest, @response, @responseRows

set @payload = '{
    "verificationCode": "123456",
    "clientId": "XXXX",
    "campaignId": "Validation",
    "target": "0000000000"
}'

set @postrequest = HTTPPost2("https://www-abc.com/api/messaging/v1/verify","application/json", @payload, false, @response, @responseRows)
RaiseError(CONCAT('The error experienced was: ',@postrequest),false)

]%%

// Output: %%=v(@response)=%%
// Header: %%=v(@responseRows)=%%
<script language="javascript" runat="server">
} catch (ex) {
     Variable.SetValue("@errorMsg", Stringify(ex));
        //Write(ex.name);
        Write(ex.message);
        Write(ex.response);
}
</script>
code: %%=v(@postrequest)=%%
response : %%=v(@response)=%%
responseRows: %%=v(@responseRows)=%%
responseRowCount: %%=RowCount(@responseRows)=%%

Below is the output for the same-

code: 400
response : ���T��� F_�ܹ1��0�1R0��[C��Sntj��9��f�Q@���Q�^���Eo��s����7F��;M1��|�2��/S�n�0%J�g4�W���2�j��g��N��������Z(&�5�������k�
responseRows: System.Data.DataRow[]
responseRowCount: 7

I am not sure why the response is corrupted in case of 4xx or 5xx response while trying with HTTPPOST2 using amp script where as for success scenario it worked perfectly fine.

Also, have tried with HTTP.POST method via SSJS as well but it seems we cannot read status code and response in case of HTTP.POST. Salesforce just throw back a generic message in case of 400:bad request and unable to read response body as well.

Below is the error we got in case of 400: bad request and reponse body is coming null. description: "

ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.

↵ Error Code: OMM_FUNC_EXEC_ERROR
↵ - from Jint --> 
↵
↵ --- inner exception 1---
↵
↵System.Net.WebException: The remote server returned an error: (400) Bad Request. - from System
↵
↵
↵
↵"

Looking forward for help on this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.