Northers
Northers

Reputation: 85

Postman is seemingly ignoring my POST in the pre-request

I am trying to set up a DELETE call, but to do this I first need to create the data to delete so I am trying to call a POST in the pre-request. I have run the POST as a normal request and it works fine, but inside the pre-request it just seems to be getting ignored. I know this because I can alter the URL to something that should not work and it makes no difference and raises no errors.

This is what I have in my pre-request: -

pm.sendRequest({
    url: "http://someurl/test",
    method: 'POST',
    header: {
        'Authorization': 'Basic Tmlfefe89899eI='
    },
    body: {     
    "ClientId": 594,
    "Name": null,
    "Disabled": false
    }, function (err, res) {
        console.log(res);
    }
});

Is there anything special I have to do to use a POST as a pre-request? Any help would be greatly appreciated.

Upvotes: 1

Views: 998

Answers (2)

Danny Dainton
Danny Dainton

Reputation: 25881

Seems strange that nothing is happening, might be returning a 400 but you're only going to see that in the Postman Console.

You can open this by hitting this icon, you'll find it in the bottom left of the app:

Postman

This is an example view from the Console, the icon next to the timing on the first request will show that a pm.sendRequest() function was used:

Postman

I would suggest just changing your request a little bit to something like the one below and it should be fine:

pm.sendRequest({
    url: 'http://someurl/test',
    method: 'POST',
    header: {
        'Authorization': 'Basic Tmlfefe89899eI=',
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({ ClientId: 594, Name: null, Disabled: false})
    }
}, function (err, res) {
    console.log(res);
});

Upvotes: 2

Divyang Desai
Divyang Desai

Reputation: 7866

There is a trick to get request information for the pre-request script.

Save the request in a separate collocation (temporary) and export that collection. And then check that json collection using IDE or notepad, you'll get all the information there, use that as it is for your request.

Using given information in the question, here is how your pre-request script looks like,

pm.sendRequest({
    url: "http://someurl/test",
    method: "POST",
    header: [{
        "key": "Authorization",
        "value": "Basic Tmlfefe89899eI=",
        "type": "text",
    },
    {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
    }],
    body: {
        mode: 'raw',
        "raw": ""raw": "{\n    \"ClientId\": 594,\n    \"Name\": null,\n    \"Disabled\": false\n}"
    }
}, function(err, res) {                
     console.log(res);    
});

Also check Postman console, you'll get all the information there, including request, response and errors if any.

Upvotes: 2

Related Questions