Date Variable in Pre-request Script causes strange error and transfers 0 to the server

I have a bit of a strange problem:

I have to pass a JSON object that contains a Date in EPOCH. This means I transfer a long value. I define the JSON object in the body as follows:

{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": "{Value1: \"Value-1 Changed\", Value2: \"Value-2\"}",
        "Expiry": "{{config_expiry}}"
    }
}

The Expiry property I set in a pre-request script as follows:

var expiry = Date.now(Date.UTC());
pm.collectionVariables.set("config_expiry", expiry);
console.log("variable config_expiry is: "+pm.collectionVariables.get("config_expiry"));

The output in the log looks perfectly fine:
variable config_expiry is: 1596034570100 If I convert that back to a human readable format, it is exactly the time I sent the request.

But on the server I get 0 as value for Expiry and if I look at the raw request I see the following:

PUT /bscadmin/v1/config.json?BscRequestMeta={CallingRequestId:23d194f5-f8f1-44e7-a9a1-348e16b1c30e,Source:%22PostmanApiTestCall%22,CallingUserId:%22postman%22,CallingUserEmail:%22postman@bedisoft.cloud%22,CallingUserIp:29.83.212.68,clientType:%22Postman%22} HTTP/1.1
Authorization: Bearer rDMh6KX3xdD6uf16Ab4ygo0kUkxb38js
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Postman-Token: 63c7a771-d626-4350-918f-1ff33fff077a
Host: jazz-linuxdev:13666
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 255
Cookie: ss-opt=temp; X-UAId=10; ss-id=Z3OtEYBs6UmKTTBW5jcu; ss-pid=qH66h8h82LGkr7UClFft
{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": "{Value1: \"Value-1 Changed\", Value2: \"Value-2\"}",
        "Expiry": "Invalid Date"
    }
}

It says: ā€œExpiryā€:"Invalid Date"

So I do not understand who returns this! Why does it talk about a date? It should simply send a number (int64 / long).

Could anybody explain me what is going wrong here?

Hey @tbednarz,

Hmm, thatā€™s quite odd. I tried to replicate your issue with Postman Echo, but the Expiry doesnā€™t display Invalid date. My initial guess would be that it could be server-side. One thing worth trying is for you to run a cURL request and see if the same issue persists?
You can use the code generation feature:
image
Iā€™d recommend trying to see if you can replicate this behaviour with the cURL request.

Also, it looks like you included a bearer token in your message. Make sure to omit these sorts of credentials when posting anywhere.

Hi Sabri,

Thanks for our reply. Regarding the Bearer Token: This is not a problem here since it is a VM on my local development computer running VMware Fusion. These tokens are re-generated daily when I restart my environment. And none of the VMs are accessible from outside of my machine (not even from my internal network).

Regarding Curl:

Thatā€™s not working at all! (I tried from the shell of my MAC as well as from the shell of a virtual Fedora 32 Workstation) Here is what I copied from Postman:

curl --location --request PUT 'http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta={CallingRequestId:e6261c04-97c7-4fa7-a21a-bf20bf3d8cff,Source:%22PostmanApiTestCall%22,CallingUserId:%22postman%22,CallingUserEmail:%22postman@bedisoft.cloud%22,CallingUserIp:213.179.15.78,clientType:%22Postman%22}' \
--header 'Authorization: Bearer rDMh6KX3xdD6uf16Ab4ygo0kUkxb38js' \
--header 'Content-Type: application/json' \
--header 'Cookie: ss-opt=temp; X-UAId=10; ss-id=T4wWr1iPwbYvpvE8dQEM; ss-pid=dtYPJXJChKNMAUHAnXD2' \
--data-raw '{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": "{Value1: \"Value-1 Changed\", Value2: \"Value-2\"}",
        "Expiry": "Invalid Date"
    }
}'

Interesting: The code which is generated by Postman for curl (and also wget) both contain a value "Invalid Date" for the property Expiry! So this looks like something inside Postman generates thisā€¦
And the result:

[1/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingRequestId:ef6c2174-354e-46c7-8316-5730bf37ec98 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingRequestId:ef6c2174-354e-46c7-8316-5730bf37ec98
{"responseStatus":{"errorCode":"SerializationException","message":"Unable to bind to request 'BscUpsertConfigItemRequest'","errors":[{"errorCode":"SerializationException","fieldName":"BscRequestMeta","message":"'CallingRequestId:ef6c2174-354e-46c7-8316-5730bf37ec98' is an Invalid value for 'BscRequestMeta'"}]}}
[2/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=Source:%22PostmanApiTestCall%22 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=Source:%22PostmanApiTestCall%22
{"responseStatus":{"errorCode":"SerializationException","message":"Unable to bind to request 'BscUpsertConfigItemRequest'","errors":[{"errorCode":"SerializationException","fieldName":"BscRequestMeta","message":"'Source:\"PostmanApiTestCall\"' is an Invalid value for 'BscRequestMeta'"}]}}
[3/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserId:%22postman%22 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserId:%22postman%22
{"responseStatus":{"errorCode":"SerializationException","message":"Unable to bind to request 'BscUpsertConfigItemRequest'","errors":[{"errorCode":"SerializationException","fieldName":"BscRequestMeta","message":"'CallingUserId:\"postman\"' is an Invalid value for 'BscRequestMeta'"}]}}
[4/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserEmail:%22postman@bedisoft.cloud%22 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserEmail:%22postman@bedisoft.cloud%22
{"responseStatus":{"errorCode":"SerializationException","message":"Unable to bind to request 'BscUpsertConfigItemRequest'","errors":[{"errorCode":"SerializationException","fieldName":"BscRequestMeta","message":"'CallingUserEmail:\"postman@bedisoft.cloud\"' is an Invalid value for 'BscRequestMeta'"}]}}
[5/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserIp:16.232.152.202 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=CallingUserIp:16.232.152.202
{"responseStatus":{"errorCode":"SerializationException","message":"Unable to bind to request 'BscUpsertConfigItemRequest'","errors":[{"errorCode":"SerializationException","fieldName":"BscRequestMeta","message":"'CallingUserIp:16.232.152.202' is an Invalid value for 'BscRequestMeta'"}]}}
[6/6]: http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=clientType:%22Postman%22 --> <stdout>
--_curl_--http://jazz-linuxdev:13666/bscadmin/v1/config.json?BscRequestMeta=clientType:%22Postman%22
[~]

Donā€™t know if it has problems with encodingā€¦

You write:

Could you please elaborate this a bit? In Postman this shows up under Request Body so I thought it did not yet talk to the server.

I currently run the server in the debugger and as I wrote, the server shows up a value of 0 for the Expiry. The server is written in C# against .NET Core 3.1. I use a library called ServiceStack which does the serialization from JSON to C#. But there I do not get any errors at all. (Just as a remark: I develop my servers under Fedora using JetBrains Rider, so no fancy windows compatibility stuff, all pure Linux. Production stuff runs as Docker containers under RHEL)

So to me it is still unclear who writes this ā€œInvalid Dateā€ thereā€¦ Is it a JavaScript problem or is there a problem with getting a variable from a Pre-request script in the Body of the request??

Hi @tbednarz,

Iā€™d check if this is a variable scoping issue. Are you using an environment that perhaps had previously set a variable named config_expiry with the output of an invalid date in a script? See more about variable scopes here: https://learning.postman.com/docs/sending-requests/variables/#variable-scopes.

One tip is that you can use the Find tab in the lower left to search through various entities inside Postman.

Best,

Kevin

1 Like

Hi Kevin, thanks for the hint regarding Find and Replace.

Hmm, not that I know, however I cannot completely exclude, that I tried a several set variants when I wrote my pre-request script. I am pretty sure I used the UI only to define the number of global variables I have, but it is possible, I tried several things when I tried setting a variable from a script.

I am designing and writing software for more than 2 decades but I am a bloody beginner with Postmanā€¦ So a lot is possibleā€¦

However after deleting the entry from the collection variables it works just fine!

Please allow for one more question: In the previous message I posted a screen shot with the Body for the request in question. Sometimes my JSON objects are a bit more complex and contain nested objects like shown there:

{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": "{Value1: \"Value-1 Changed\", Value2: \"Value-2\"}",
        "Expiry": "{{config_expiry}}"
    }
}

The property JsonValue is just a very simple sample. I noticed that I cannot ā€˜nice printā€™ nested objects like so:

{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": {
            Value1: Value-1 Changed, 
            Value2: Value-2
         },
        "Expiry": "{{config_expiry}}"
    }
}

If I do this, the data is added exacly like shown above with tabs, spaces, CR/LF etc. Is this due to the fact, that I serialize into a string on the server side?

Just as little background: The server is written in C# and .NETCore. I develop it on Fedora with JetBrains Rider. The back-end DB is in this case MongoDB and the data contained in the property JsonValue is later converted to a MongoDB C# driver specific data type BsonDocument. I use this if I like to have dynamic, complex data stored under one property.

I just wonder if it is possible to write this in a more readable fashion in Postman than put everything on one single line, quote special characters etc.

Hi @tbednarz,

I would look at sending it as a regular JSON object and de-serializing it as a string on the server side. If youā€™re piping this directly into a BsonDocument.Parse call, Iā€™d also exercise caution, as I donā€™t know if that function sanitizes JSON. I imagine System.Text.Json handles some basic sanitizing of the data. Your request body would end up looking like this:

{
    "ConfigurationItem": {
        "Namespace": "system:test",
        "KeyName": "UpsertTests",
        "InstanceName": "jazz-linuxdev",
        "JsonValue": {
            "Value1": "Value-1 Changed", 
            "Value2": "Value-2"
         },
        "Expiry": "{{config_expiry}}"
    }
}

Best,

Kevin