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

HTTP code generation doesn't handle variables in URL correctly #315

Closed
andrewhickman opened this issue Aug 7, 2020 · 7 comments · Fixed by #397
Closed

HTTP code generation doesn't handle variables in URL correctly #315

andrewhickman opened this issue Aug 7, 2020 · 7 comments · Fixed by #397
Assignees

Comments

@andrewhickman
Copy link

andrewhickman commented Aug 7, 2020

Describe the bug

The HTTP code generator, when given a URL starting with a variable like {{my_base_url}}/path, generates code like

GET /path HTTP/1.1
Host: https://example.com/base

Note the host header incorrectly includes both the scheme and part of the path, while the actual path is incomplete

Expanding the variable in manually causes the generator to give the correct output

GET /base/path HTTP/1.1
Host: example.com

To Reproduce
Steps to reproduce the behavior:

  • Set the my_base_url variable to https://example.com/base
  • Create a GET request with the url set to {{my_base_url}}/path
  • View the generated HTTP code

Screenshots

For example you could add the screenshot of the snippet/request builder from Postman app.
image

@VallariAg
Copy link

I'll like to work on this! Could you please assign it to me?

@VallariAg
Copy link

On the application, I noticed this is a general issue and not just with http codegen.
Here is the options in NodeJs - Native snippet:

var options = {
  'method': 'GET',
  'hostname': 'https://example.com/base',
  'path': '/path',
  'headers': {
    'Content-Type': 'application/json'
  },
  'maxRedirects': 20
};

The convert function takes in a request parameter which is a PostmanRequest object made by postman-collection.
I think the issue lies somewhere in postman-collection's Request function and how it handles a string URL.

If this sounds right, should I open an issue on the postman-collection repo?

@umeshp7
Copy link
Member

umeshp7 commented Sep 22, 2020

@VallariAg Yes seems like it, I observed the same behavior. Able to reproduce this in Python - http.client, NodeJs - native and HTTP.
When exported the collection found this in that request:

"url": {
   "raw": "{{url}}/check",
       "host": [
	     "{{url}}"
	],
	"path": [
		"check"
	]
}

@codenirvana Thoughts?

@codenirvana
Copy link
Member

@umeshp7 If your use case requires replacing variables in a parsed URL object, always:
Convert it back to a string -> replace variables -> parse again.

@umeshp7
Copy link
Member

umeshp7 commented Oct 1, 2020

@codenirvana The logic for replacing the variables isn't in code-generators, we use the information directly provided to us in the sdk's url object. https://github.com/postmanlabs/postman-code-generators/blob/develop/codegens/http/lib/util.js#L170

@webholik
Copy link
Collaborator

@codenirvana We are getting wrong url from Runtime when the URL starts with a variable.
For example if you have a variable, say path = https://example.com/path and you use it in the address:
Screenshot_20201023_132200

This is the url object that is passed to the code-generator:

"url": {
    "path": [
      "hello"
    ],
    "host": [
      "https://example.com/path"
    ],
    "query": [],
    "variable": []
  }

As you can see it is taking the value of the variable as Host and the rest as path.

@codenirvana
Copy link
Member

@umeshp7 @webholik If you are resolving variables in a URL (not sure how it's done in code-gen) you have to parse it again.
That's how it's done in postman-runtime also.

So, if the input source is not guaranteed to provide a proper parsed URL, I suggest always parsing it again in the code generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants