-
Notifications
You must be signed in to change notification settings - Fork 839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change request body in pre-request script #4808
Comments
Related to #4631 |
I have many request all is this function to encrypt. So, I created a collection for these requests. I hope the encrypt and decrypt scripts in the collection. Like this: |
+1 |
Hi numaanashraf, Storing the value in the environment variable and manipulating that in pre-request Script and assigning that environment variable to the body. Thanks. |
We added mutating URLs and headers for requests from within pre-request script ... mutating other parts like auth and body (to some extent) would follow in short to mid term. :-) Meanwhile, in UI we need to have ways to see what exactly was sent (since programmatic changes are hard to spot.) |
@madmax911 Your solution was very helpfull for me. Thanks |
I've just hit this problem too. It would be great if there were hooks for after the environment variables had been set and the request was ready to send. I had to generate a signature based on the request body and while the environment variable solution I need to apply a signature to every request in my collection which is why the I ended up taking the body and doing a manual string replace to generate the signature and let the environment variable do its thing later in the postman request cycle. eg.
In my case, even a function to parse the body and place the environment variables would be handy, even if it didn't mutate the actual body. That would mean I get consistent variable replacement and don't have to use my own hacky string replace. There may be a function which does this, but i'm not too familiar with postman and couldn't find one. |
Not sure if this is related but I am running into same problem. I need a way to paste a raw http request string like below and generate postman request.
What I do currently is paste this whole HTTP request as request body and using JS I parse the HTTP request and completely reconstruct the request and send it. It works but it's all too magical as I can not see the request in the UI. Would appreciate if anyone have solved this? |
@arlemi is there anywhere where we can vote up this feature so it can have higher priority? |
@lvl99 You can upvote by adding a 👍 on the first comment in this issue. |
priority 👍👍👍
|
My use case is injecting an "extensions" key into a GraphQL query request (yes, I realize that is not mentioned in the GraphQL spec; "extensions" is part of response, but not request). If we want to use Postman's GraphQL editor, then the workaround mentioned above will not allow us to do so. For now, my workaround is running a local Express.js proxy server that transforms request bodies before sending it off to the actual server. |
this would be very helpfull. I would like to use this to remove comments from the json request body. (because this isn't possible as well, see #3358 ) But I really don't like the workaround to put all the request data in the pre-request-script and just set the body from a variable. It is good to have this workaround, but it still is just a workaround. |
Linking this here, because it is a better description of the actual bug/enhancement (I do not agree with it being closed as a duplicate). #8466. Can someone please provide an update on this ticket. Thanks. |
2 years after the creation of issue and i'm here searching for the same thing. |
IMO, this bug should be closed, and #8466 reopened. This thread does not give a concise description of what many of us are looking for. @codenirvana |
Hello everyone 👋 Example to set different body modes: // Raw
pm.request.body.update('Hello World!');
// Form Data
pm.request.body.update({
mode: 'formdata',
formdata: [{
key: 'foo',
value: 'bar'
}]
});
// URL Encoded
pm.request.body.update({
mode: 'urlencoded',
urlencoded: [{
key: 'foo',
value: 'bar'
}]
});
// GraphQL
pm.request.body.update({
mode: 'graphql',
graphql: {
query: `
query Square($ten: Int!) {
square(n: $ten)
}
`,
variables: { ten: 10 }
}
}); Example to mutate existing request body: // Send Base64 encoded body
const rawBody = pm.request.body.toString();
const base64Body = btoa(rawBody);
pm.request.body.update(base64Body); // Add a new param
pm.request.body.formdata.add({
key: 'foo',
value: 'bar'
}); // Strip JSON Comments
const rawData = pm.request.body.toString();
const strippedData = rawData.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m)
pm.request.body.update(strippedData); Please try it out and let us know if you face any issues. |
Damn, need a micdrop emoji with that closed issue message 🎤 💥 |
Hell yes. Thank you!!! |
A example to add json parameter on raw body:
|
Just yesterday I was looking into how to update the body in pre-request scripts. My example is getting data no older than 30 days. This would have to update every day, hence the need to update the request body. Thank you for adding this feature. Works great. 😁👍🍾🥂 |
For creating a request body from scratch, it makes sense to take this approach. I think this can be simplified while updating existing JSON. Not sure if this is the intended usage, but the output seems to be the same.
Correct me if I am wrong @codenirvana |
Following code did it for me:
|
@codenirvana Hi, is it possible to add a method to change the response too?
|
@coder-free Can you please provide a use-case where you need to update the response? |
@shubhbhargav
Now my Tests script is:
I just want to see the decrypted result conveniently. This need is not strong. Because I can also see the result from the console log. It's just not that convenient. Thanks. |
This is interesting. Thanks for sharing it @coder-free. I would like to recommend an alternate to console (I know it's not ideal). The idea of visualizer is to render customized responses, similar to this use-case. Let me know if that serves your use-case. :) |
You have two options - one, shubhbhargav, describes above. Your second option is to send your request in a pre-request script, parse the response, decrypt your string, and call pm.request.body.update() to be sent to the https://postman-echo.com/post API. I have an example of each, let me know if you would like to see an example of the second option I just described. Here is an example of the first option, which will automatically copy your string to clipboard as well..:
`let template = {{insert backtick here}};`
(Sorry for all the post edits.. Not sure how to write nested backticks in markup...) |
@shubhbhargav @StrohmyerTR Thanks. Visualizer is a great solution. Using it not only gives me the results I want. More additional features can be implemented if more time is invested. |
Hey! I'm struggling with setting the My request looks like this: pm.sendRequest("https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Neckertal_20150527-6384.jpg/640px-Neckertal_20150527-6384.jpg", function (err, response) {
const stream = response.stream;
// const data = response.dataURI();
// set Form Data request body
pm.request.body.update({
mode: 'formdata',
formdata: [
{
key: 'id',
value: pm.variables.replaceIn('{{$randomUUID}}')
},
{
key: 'file',
type: 'file',
value: stream // this is not working: "Not a valid image file"
// value: data // this is not working: "Not a valid image file"
}
]
});
}); How is it possible to set the file of a formdata using pre-request scripts? |
@chris-rl I guess the question here is how to submit a form-data request to upload a file without providing a file. AFAIK, this is not possible, not even from a regular Postman request. I don't know a way to upload a file by directly providing the content. Correct me if I am wrong. |
@vdespa Yes, that's probably true. I'm not too much into the technical details on how form-data requests are working internally. But I guess at some point the file will be loaded and sent to the endpoint, right? There should be a way of doing this manually. Maybe another idea would be to save the downloaded file temporarily and then use the path to the temp file 🤔 |
@chris-rl I think the expectation is that the file exists somewhere on the disk. However, you can't write files from Postman, unless you rely on an externals script that does that for you. |
I want crypt the request body.
![image](https://user-images.githubusercontent.com/2630290/42554373-32d707e0-8517-11e8-80c0-64726b232b1b.png)
For example,
request body set to this:
and pre-request script set to this:
![image](https://user-images.githubusercontent.com/2630290/42554480-a3d8c0a0-8517-11e8-9b39-b64d5989df6e.png)
and logged the "encryptData" value is encrypted. But the server received data is not encrypt, still is '{"a":"b"}'
The text was updated successfully, but these errors were encountered: