0

I would like to know if there is a way to retrieve an HTTP request method from the HTTP request headers. Based on the method used (GET, POST, ...) I will run a different logic on my Cloud Page.

If I look at the source of my request headers, the information is there, but it doesn't have any attribute that can be used in HTTPRequestHeader() or GetRequestHeader(). So, can I somehow retrieve the first line of the following header that tells me what request method has been used?

enter image description here

1 Answer 1

6

You're looking for Platform.Request.Method(), that's listed in the HTTP Properties Functions article.

Here's an example snippet that will extract most of the data you might need:

<script runat="server">
Platform.Load("core", "1");

var requestData = {
        Method: Platform.Request.Method(),
        Browser: Platform.Request.Browser(),
        UserAgent: Platform.Request.UserAgent(),
        ClientIP: Platform.Request.ClientIP(),
        HasSSL: Platform.Request.HasSSL(),
        IsSSL: Platform.Request.IsSSL(),
        QueryString: Platform.Request.QueryString(),
        ReferrerURL: Platform.Request.ReferrerURL(),
        RequestURL: Platform.Request.RequestURL(),
        UserAgent: Platform.Request.UserAgent(),
        Authorization: Platform.Request.GetRequestHeader('Authorization'),
        PostData: Platform.Request.GetPostData('utf-8')
    }

Write(Stringify(requestData))
</script>

For example a request from Postman will get something like this in response:

{
    "Method": "PATCH",
    "Browser": {
        "Platform": "Unknown",
        "Browser": "Unknown",
        "Version": "0.0",
        "MajorVersion": 0,
        "MinorVersion": 0
    },
    "UserAgent": "PostmanRuntime/7.31.3",
    "ClientIP": "127.0.0.1",
    "HasSSL": true,
    "IsSSL": true,
    "QueryString": "",
    "ReferrerURL": null,
    "RequestURL": "https://subdomain.yourdomain.com/your-page",
    "Authorization": "Basic SGVsbG86U0ZTdGFja0V4Y2hhbmdl",
    "PostData": "{\r\n    \"hello\": \"world\"\r\n}"
}
5
  • This is awesome, Rafał! Serdeczne dzięki :) Mar 21, 2023 at 9:58
  • Mate, this is exactly what I was looking for. Thank you!
    – Hroosha
    Mar 22, 2023 at 1:37
  • Glad you both liked it :) Mar 22, 2023 at 8:34
  • 1
    The first code sample is missing Platform.Load("core", "1"); May 8, 2023 at 15:15
  • @AdamSpriggs thanks for pointing that out - it was a strange omission on my part now that I look at it. I've added that line as you suggested May 9, 2023 at 3:50

You must log in to answer this question.

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