postman-code-generators icon indicating copy to clipboard operation
postman-code-generators copied to clipboard

Added powershell here strings to powershell-restmethod codegen

Open karmanya007 opened this issue 5 years ago • 9 comments

Fixes #322 . Got rid of the escapes by using a PowerShell here-strings in powershell-restmethod codegen.

Request 1

method: 'POST',
    header: [
        {
            key: 'Content-Type',
            value: 'text/plain' 
        }
    ],
    url:{
        raw: 'https://mockbin.org/request',
        protocol: 'https',
        host: [
            'mockbin',
            'org'
        ],
        path: [
            'request'
        ]
    },
    body: {
        'mode': 'raw',
        'raw': 'Hello world'
    },
    description: 'Description'

Conversion

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "text/plain")

$body = @"Hello world"@

$response = Invoke-RestMethod 'https://mockbin.org/request' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Request 2

method: 'POST',
    header: [],
    body: {
        mode: 'graphql',
        graphql: {
            query: '{ body { graphql } }',
            variables: '{"variable_key": "variable_value"}'
          }
        },
    url: {
        raw: 'http://postman-echo.com/post',
        protocol: 'http',
        host: [
            'postman-echo',
            'com'
        ],
        path: [
            'post'
        ]
    }

Conversion

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = @"{"query":"{ body { graphql } }","variables":{"variable_key":"variable_value"}}"@

$response = Invoke-RestMethod 'http://postman-echo.com/post' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

karmanya007 avatar Oct 24 '20 14:10 karmanya007

@umeshp7 All checks have passed and I have added the sample outputs.

karmanya007 avatar Oct 25 '20 17:10 karmanya007

@karmanya007 I have enabled newman tests for poweshell codegen. These tests run the generated snippet in powershell and compare the output with that generated by newman. You can see in the CI logs that some of the tests are failing due to incorrect escaping of characters in the string. We have to get those tests to pass before we can merge your changes.

webholik avatar Oct 28 '20 16:10 webholik

@karmanya007 I have enabled newman tests for poweshell codegen. These tests run the generated snippet in powershell and compare the output with that generated by newman. You can see in the CI logs that some of the tests are failing due to incorrect escaping of characters in the string. We have to get those tests to pass before we can merge your changes.

@webholik On it!

karmanya007 avatar Oct 28 '20 17:10 karmanya007

@karmanya007 Any updates here? Looks like there are some conflicts.

umeshp7 avatar Nov 03 '20 06:11 umeshp7

@umeshp7 Had a marriage in my family so I was not active as much. I know the fix. Just have to implement it. Will do so shortly.

karmanya007 avatar Nov 03 '20 09:11 karmanya007

Getting these three errors. @Toronto00 , @umeshp7 Could you guide me in the correct direction on how to approach these conflicts. 1 2 3

karmanya007 avatar Nov 13 '20 11:11 karmanya007

@webholik Are the 1st and 3rd error from wrong newman tests, maybe?

karmanya007 avatar Nov 15 '20 08:11 karmanya007

@karmanya007 1st error is because you are not escaping $ character in the output string. If $ is present in a string then Powershell tries to replace it with a variable. In this case it is replacing it with the value of $test, which isn't defined, so it is just putting empty string here.

2nd error again is just wrong escaping. ` character must be properly escaped.

3rd has been fixed in #421. You should rebase your PR on top of it.

webholik avatar Nov 17 '20 09:11 webholik

@karmanya007 We recently pushed changes to use here-strings in Powershell. Could you check if this PR would still be required?

dhwaneetbhatt avatar Mar 28 '23 10:03 dhwaneetbhatt