0

I trying to make a REST API POST request to an external Application from the Cloud page But getting 500 internal error. Can you please help me out with this on what mistake I'm doing

%%[
    set @uname = "Harish"
    set @pwd = "Password"
    set @auth = Concat(@uname, @pwd)
    set @url = "https://endpointurl.com"
    
    set @payload = '{
        "addSeminarCustomerInputField":{
            "seminarCustomerField":{
                "additionalAttendeesField":0,
                "confirmedField":true,
                "customer_IDField":1170, 
                "isHandicappedField":false,
                "isHearingImpairedField":false,
                "isVisionImpairedField":false,
                "notesField":"Test Notes",
                "seminar_IDField":3
            }
        }
    }'
     
      SET @req = HTTPPost2(@url, "application/json", @payload, false, @response, "Authorization", @auth)
      
      
     ]%%
    
    Res = %%=v(@response)=%%

enter image description here

Postman code:

curl --location --request POST 'https://endpointurl.com' \
--header 'Authorization: Basic Q0JDX0FQSTpjYmNhcGkyMDIw' \
--header 'Content-Type: application/json' \
--header 'Cookie: ASP.NET_SessionId=on4xa3ow4hta45lipltm5x0w' \
--data-raw '{
    "addSeminarCustomerInputField":{
        "seminarCustomerField":{
            "additionalAttendeesField":0,
            "confirmedField":true,
            "customer_IDField":1170,
            "isHandicappedField":false,
            "isHearingImpairedField":false,
            "isVisionImpairedField":false,
            "notesField":"Test Notes",
            "seminar_IDField":3
        }
    }
}'
2
  • Have you verified if the call is correct? If the post does not return 200 status, then the whole page will toss an exception Sep 23, 2020 at 21:47
  • @Gortonington: Yes. I test in Postman and it worked and returned 200 status.
    – Harish
    Sep 23, 2020 at 22:09

1 Answer 1

1

I believe the issue is because in your HTTPPost2 function itself. Based on the documentation the parameters are as followed:

parameters

Please pay attention to parameter 5 and 6 in your code:

SET @req = HTTPPost2(@url, "application/json",  @payload, false, "Authorization", @auth,  @response)

You have moved your @response variable to #7 The correct way would be:

SET @req = HTTPPost2(@url, "application/json",  @payload, false, @response, "Authorization", @auth)

Edit:

My code example was missing parameter 6 as I have described above:

SET @req = HTTPPost2(@url, "application/json",  @payload, false, @response, @responseRowSet, "Authorization", @auth)

ResponseRowSet need to be set as parameter 6 not your first header value.

Also, you should review Authentication vs Authorization. If you stick with Authorization please have a read at here

You may want to change your @auth variable to:

set @auth = Concat('Basic ',Base64Encode(Concat(@uname,':',@pwd)))
6
  • I tried according to your comments, but no luck. Still the same error.
    – Harish
    Sep 24, 2020 at 1:23
  • updated my post
    – shd.lux
    Sep 24, 2020 at 2:01
  • I have updated it according to your comments. I got an Access Denied response (I have attached the response screenshot above). If it's because of user ID & Password, but it worked in postman with the same credentials.
    – Harish
    Sep 24, 2020 at 4:53
  • As your code is rendering fine, the issue is not with the code itself but a logical issue with your endpoint. This is out of scope for salesforce.stackexchange. What I would suggest is that you take your working Postman call and click on "Code" on the top right hand side. You will be able to see the actual call and what you may need to change in your header to achieve the same result
    – shd.lux
    Sep 24, 2020 at 8:02
  • I have updated the post with the working Postman code. What changes do you think I should make?
    – Harish
    Sep 24, 2020 at 17:29

You must log in to answer this question.

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