Skip to content
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

Enable pre- and post-request scripting support. #154

Merged
merged 4 commits into from
Aug 20, 2024
Merged

Conversation

gorillamoe
Copy link
Member

Enable pre- and post-request scripting support.

Example inline

< {%
// some inline javascript pre-request script
%}
GET https://httpbin.org/get HTTP/1.1

> {%
// some inline javascript post-request script
%}

Example file

< ./pre-process.js
GET https://httpbin.org/get HTTP/1.1

> ./post-process.js

This matches the this behaviour.

@gorillamoe gorillamoe added the enhancement New feature or request label Aug 18, 2024
@gorillamoe gorillamoe self-assigned this Aug 18, 2024
@gorillamoe gorillamoe marked this pull request as draft August 18, 2024 15:40
@gorillamoe gorillamoe force-pushed the feat/scripts branch 2 times, most recently from 6be2001 to e9a29b4 Compare August 18, 2024 18:06
@gorillamoe gorillamoe force-pushed the feat/scripts branch 2 times, most recently from 024372b to e179d95 Compare August 20, 2024 07:59
@iamxiaojianzheng
Copy link
Contributor

Is there support for using libraries in scripts? Like moment.

In postman, I can use it like this.

const randomDate = require('moment')().format('YYYY-MM-DD HH:mm:ss')

@gorillamoe
Copy link
Member Author

Is there support for using libraries in scripts? Like moment.

In postman, I can use it like this.

const randomDate = require('moment')().format('YYYY-MM-DD HH:mm:ss')

If you have that package 📦 installed globally 🌎 or local to that .http in node_modules folder, then yes.

@gorillamoe gorillamoe marked this pull request as ready for review August 20, 2024 22:56
@gorillamoe gorillamoe merged commit aa19e12 into main Aug 20, 2024
@gorillamoe gorillamoe deleted the feat/scripts branch August 20, 2024 22:57
@gorillamoe
Copy link
Member Author

gorillamoe commented Aug 20, 2024

Is there support for using libraries in scripts? Like moment.

In postman, I can use it like this.

const randomDate = require('moment')().format('YYYY-MM-DD HH:mm:ss')

Should work like that: https://kulala.mwco.app/docs/scripts/overview#using-node-modules

Given a folder struct like this:

/home/marco/projects/personal/kulala.nvim/https
.
├── one
│   ├── foo.http
│   ├── http-client.env.json
│   ├── product-queries.graphql-schema.json
│   ├── product-queries.http
│   └── test.http
├── scripts
│   ├── node_modules
│   │   └── moment
│   ├── package.json
│   ├── package-lock.json
│   └── script.js
└── two
    ├── foo.http
    ├── http-client.env.json
    ├── product-queries.graphql-schema.json
    ├── product-queries.http
    └── test.http

6 directories, 13 files

I can run this https/one/test.http file with the following contents:

# @name REQUEST_ONE
POST https://httpbin.org/post HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer Foo:bar

{
  "foo": "bar"
}

> ./../scripts/script.js

###

# @name REQUEST_TWO
POST https://httpbin.org/post HTTP/1.1
accept: application/json
content-type: application/json

{
  "token": "{{REQUEST_ONE.response.body.$.json.foo}}",
  "gorilla": "{{RANDOM_DATE}}"
}

And the contents of script.js

const randomDate = require('moment')().format('YYYY-MM-DD HH:mm:ss');
client.global.set('RANDOM_DATE', randomDate);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants