0

I Postman I am Preparing a POST Request.

I need to pass an Entity ID, which I need to get from and Entity Name. Trying to use Pre-request Script for that.

The Prescript fails, as body can't be parsed. I can see in the Console that the body is returned. Response Headers says: Content-Encoding: gzip and Content-Type: application/json.

What I am doing wrong here?

console.log('Response body (GET 1):', response.body); gives " Response body (GET 1): undefined

My Prescript

// Set the entity names
var fqn1 = 'xyz'; 
var fqn2 = 'xyz';

// Initialize variables to store the IDs
var entityId1, entityId2;


// Perform the first GET request
pm.sendRequest({
  url: `https:///${fqn1}`,
  method: 'GET',
  header: {
    'Authorization': `Bearer ${pm.globals.get('token')}`,
    'Accept': 'application/json',
  }
}, function (err, response) {
  if (err) {
    console.log(err);
  } else {

    // Print the response body to the console for verification
    console.log('Response body (GET 1):', response.body);

    // Extract the ID from the GET response
    var responseJson = JSON.parse(response.body);
    entityId1 = responseJson.id;

    // Log the postData to the console
    console.log(entityId1);

    // After the first GET request, perform the second GET request
    pm.sendRequest({
      url: `https:///${fqn2}`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${pm.globals.get('token')}`,
        'Accept': 'application/json',
        'Content-Type': 'application/json'
        // Add any other required headers for the GET request
      }
    }, function (err, response) {
      if (err) {
        console.log(err);
      } else {


    // Print the response body to the console
    console.log('Response body (GET 2):', response.body);

        // Extract the ID from the GET response
        var responseJson = JSON.parse(response.body);
        entityId2 = responseJson.id;

        // Log the postData to the console
        console.log(entityId2);

        // Update the POST request payload with the extracted IDs
        var postData = {
          edge: {
            fromEntity: {
              id: `${entityId1}`,
              type: 'table'
            },
            toEntity: {
              id: `${entityId2}`,
              type: 'table'
            },
            description: 'edge description',
          }
        };

        // Log the postData to the console
        console.log(postData);

        // Update the POST request body with the modified payload
        pm.request.body.raw = JSON.stringify(postData);
      }
    });
  }
});

Thank you

Dovile K.
  • 27
  • 5

0 Answers0