4

How do I programmatically get the content length of a Postman request INCLUDING variable values?

I need to include the content-length header in a PUT request I'm putting together in Postman. I can get the request content length in a pre-request script, however, according to the Postman script documentation, the pre-request script is (duh) getting the content length prior to the request being generated (or only sent?). This is problematic for my scenario, because I need the content length AFTER the variable values are filled in for the request.

//Get request length
var rdata = request.data;
var requestLength = JSON.stringify(JSON.parse(rdata)).length;
pm.environment.set("requestLength", requestLength);
console.log(requestLength);

content-length header

bynary
  • 841
  • 2
  • 11
  • 26
  • Could you grab the response header in the request and have that value assigned to the variable? Like `pm.environment.set('requestLength', pm.response.headers('Content-Length'))` ? – Danny Dainton Dec 18 '17 at 18:09
  • 3
    I actually tried this and realised it doesn’t do what I think it does - `pm.response.headers` returns an array of the headers. You could do JSON.stringify() on that and then JSON.parse() our the value you need. It’s very similar to what you have though. You could try this ([4] is from my own request, might need to change this)? `var contentLength = JSON.stringify(pm.response.headers); pm.environment.set('contentLength', JSON.parse(contentLength)[4].value);` – Danny Dainton Dec 19 '17 at 19:00
  • As a workaround have you tried to do a first 'dummy request' (use HEAD instead of PUT) in order to get the data length in the 'Tests' part, set your global variable, and then calling your true request with the correct value ? but it's not very clean. – A.Joly Dec 21 '17 at 08:29
  • Or you could get your request size in the tests tab, and, on a second time, try to compute it from the elements you have (body, headers,etc.) for your final request ? – A.Joly Dec 21 '17 at 08:32

2 Answers2

0

pm.environment.set("requestLength", request.data.length);

RainVision
  • 81
  • 1
  • 4
  • 2
    From Review:  Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users also with similar issues. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Jan 22 '21 at 11:31
-1

you can use this in pre-request script:

console.log(pm.request.size());

it will print the real content length for the sent request as follows: sample of the size of the request size

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33568078) – Eyeslandic Jan 06 '23 at 22:12