skip to Main Content

I have a request which need to be sent in Postman as urlencoded value like in the following (I changed some value just to be sure is not sensitive data)

notifications%3D%0A%5B%7B%22clientFileUuid%22%3A%20%22123456-fc21-496d-96d4-dfgfdgdfgfgd%22%2C%22id%22%3A%2212345678%22%2C%20%22date%22%3A%20%222023-12-04T09%3A31%3A02.678871Z%22%20%2C%22event%22%3A%22ACCEPTATION%22%2C%20%22externalId%22%3A%20%22222222222-fc21-496d-96d4-a00bd36a0be0%22%7D%5D

The same need to be prepared in its decoded form which is

notifications=
[{"clientFileUuid": "123456-fc21-496d-96d4-dfgfdgdfgfgd","id":"12345678", "date": "2023-12-04T09:31:02.678871Z" ,"event":"ACCEPTATION", "externalId": "222222222-fc21-496d-96d4-a00bd36a0be0"}]

How can I prepare the decoded request to be sent in encoded form like above?
Is there any pre-request script we can configure to achieve the goal?

I tried with x-www-form-urlencoded variables, I tried to create a request in the pre-request but I’m not succeeding.
In general this:
notifications=[{"field" : "value"}] it comes difficult to me to be configured.
Any idea ?

2

Answers


  1. Use JSON.stringify() to convert to JSON, then encodeURIComponent() to URL-encode it.

    const notifications = [{
      "clientFileUuid": "123456-fc21-496d-96d4-dfgfdgdfgfgd",
      "id": "12345678",
      "date": "2023-12-04T09:31:02.678871Z",
      "event": "ACCEPTATION",
      "externalId": "222222222-fc21-496d-96d4-a00bd36a0be0"
    }];
    let encoded = encodeURIComponent("notifications=n" + JSON.stringify(notifications));
    console.log(encoded);
    Login or Signup to reply.
  2. // First Define your JSON object, example below
    var jsonData = [{
    "id": "12345678",
    "date": "2023-12-04T09:31:02.67",
    "externalId": "222222222-fc21-496d-96d4-a00bd36a0be0"
    }];

    // Convert JSON object to string
    var jsonString = JSON.stringify(jsonData);

    // Encode the string as URL encoded format
    var encodedString = encodeURIComponent("notifications=" + jsonString);

    // Set the encoded string as the request body
    pm.request.body({
    mode: ‘urlencoded’,
    urlencoded: [{key: ‘notifications’, value: encodedString}]
    });

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search