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

Nodejs parsing for query params #249

Closed
natalie-o-perret opened this issue Dec 29, 2023 · 5 comments
Closed

Nodejs parsing for query params #249

natalie-o-perret opened this issue Dec 29, 2023 · 5 comments

Comments

@natalie-o-perret
Copy link

natalie-o-perret commented Dec 29, 2023

🙋‍♀️ Hello, it's again wee me!

Context

I have to deal some API (not mine), and the query params can pretty verbose I found only two ways to handle this case but none of them make things conveninent from my point of view:

Workarounds

Workaround 1: Full verbose variables

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
@id1=21720000
@id2=21100000
@id3=21100100
@id4=21300000
@id5=21200000
@id6=21302000
@id7=21310000
@id8=24020500
@id9=24020598
@id10=24020599
@id11=24020000
@id12=24020300
@id13=12590000
@id14=12110000
GET {{apiHost}}/api/v1/entities
    ?id={{id1}}
    &id={{id2}}
    &id={{id3}}
    &id={{id4}}
    &id={{id5}}
    &id={{id6}}
    &id={{id7}}
    &id={{id8}}
    &id={{id9}}
    &id={{id10}}
    &id={{id11}}
    &id={{id12}}
    &id={{id13}}
    &id={{id14}}
Authorization: Bearer {{getExchangeToken.access_token}}

But as you can see, it's super verbose and time-consuming to type all the ids😕

Workaround 2: Nodejs script

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
GET {{apiHost}}/api/v1/entities?{{[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598].map(x => `entityId=${x}`).join('&')}}
Authorization: Bearer {{getExchangeToken.access_token}}

So this works, but I see 2 main caveats:

  • I would rather like to have the array as a variable and then use it in the nodejs segment
  • The syntax doesn't support having the query params on another different line (which can make combining with another type of parameter again on the same line), i.e.,
    • While this works:
    GET {{apiHost}}/api/v1/entities?{{[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598].map(x => `id=${x}`).join('&')}}
    • This doesn't:
    GET {{apiHost}}/api/v1/entities
        ?{{[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598].map(x => `id=${x}`).join('&')}}

Proposal

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
@ids=[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598]
GET {{apiHost}}/api/v1/entities
    ?{{{{ids}}.map(x => `id=${x}`).join('&')}}
Authorization: Bearer {{getExchangeToken.access_token}}

But it doesn't seem to work 🤔

If something similar is already available, please feel free to point me towards that solution.

Also would love some support for multiple lines variable definitions, e.g.,

@ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]

Wdyt?

@AnWeber
Copy link
Owner

AnWeber commented Dec 29, 2023

using NodeJS Script should work.

{{ 
  exports.ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]
}}
GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

You could rename your apiHost variable to host and just write GET /api/...

Multiple line variable definitions are really hard to parse without explicit end separator. Is a empty line or comment a end of variable definition or just some formatting modification. This parsing would be unstable. I would just use NodeJS Scripts for this use case.

@natalie-o-perret
Copy link
Author

Hey there @AnWeber, thanks for being responsive, you're amazing!

So about what you just suggested:

using NodeJS Script should work.

{{ 
  exports.ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]
}}
GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

It kinda works, but there is some hiccup:

It works if the ? query param prefix is one the same line as the host stuff, i.e.,

GET {{apiHost}}/api/v1/entities?{{ids.map(x => `id=${x}`).join('&')}}

leading effectively to (when evaluated by httpyac):

GET https://my-api.com/api/v1/entities?id=21720000&id=21100000&id=21100100&id=21300000&id=21200000&id=21302000&id=21310000&id=24020500&id=24020598

That being said, when it's on new line:

GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

It ends being interpreted:

GET https://my-api.com/api/v1/entities
accept-encoding: gzip, deflate, br
accept: */*
content-length: 3458
user-agent: httpyac

    ?id=21720000&id=21100000&id=21100100&id=21300000&id=21200000&id=21302000&id=21310000&id=24020500&id=24020598
Authorization: Bearer my-amazingly-long-bearer-token

It's like the extra line messing things which oddly-enough isn't the case with the "Workaround 1: Full verbose variables" I mentioned in my original post, do you know what that might be the case?

@AnWeber
Copy link
Owner

AnWeber commented Dec 29, 2023

Yes, I just noticed that too, the parser for the URL is probably not quite right. I'll have to have a look at the regex.
One more addition, I also have a helper method that makes it easier to create query params, but it can only create arrays with the next release. I haven't needed it yet and am currently adding it.

{{ 
const {utils} = require("httpyac");
  exports.q=utils.toQueryParams({ id: [
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]})
}}
GET http://postman-echo.com/get?{{q}}

AnWeber added a commit to AnWeber/httpyac that referenced this issue Dec 29, 2023
@natalie-o-perret
Copy link
Author

Yes, I just noticed that too, the parser for the URL is probably not quite right. I'll have to have a look at the regex. One more addition, I also have a helper method that makes it easier to create query params, but it can only create arrays with the next release. I haven't needed it yet and am currently adding it.

{{ 
const {utils} = require("httpyac");
  exports.q=utils.toQueryParams({ id: [
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]})
}}
GET http://postman-echo.com/get?{{q}}

Nice! Can't wait 🪩💃!

@AnWeber
Copy link
Owner

AnWeber commented Dec 29, 2023

I released a update containing these fixes.

@AnWeber AnWeber closed this as completed Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants