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

The generated powershell script executed and lost content of some properties #6775

Closed
chucklu opened this issue Jun 24, 2019 · 7 comments
Closed
Assignees

Comments

@chucklu
Copy link

chucklu commented Jun 24, 2019

Describe the bug

The following generated powershell script executed,and lost content for logisticsRecord property

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", 'text/html')
$headers.Add("Content-Type", 'application/x-www-form-urlencoded')

$body = "billCode=75132077503374"

$response = Invoke-RestMethod 'https://hdgateway.zto.com/WayBill_GetDetail' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Result

{
  "result": {
    "prescriptionStatus": 5,
    "logisticsRecord": [
      " ",
      "  ",
      "   "
    ],
    "billPrescription": "1天17小时",
    "billCode": "75132077503374"
  },
  "message": "快递信息查询成功",
  "status": true,
  "statusCode": null
}

To Reproduce
Steps to reproduce the behavior:

  1. turn on experimental codegen mode through setting-->general
  2. run the following http post method

POST /WayBill_GetDetail HTTP/1.1
Host: hdgateway.zto.com
Accept: text/html
Content-Type: application/x-www-form-urlencoded

billCode=75132077503374

  1. code --> Generated code for Powershell - RestMethod
  2. save the code as .ps1 file
  3. run the .ps1 file by powershell, then you will get the result

Expected behavior
Get the full information like http post

Screenshots
The full information get from http post directly
image

App information (please complete the following information):

  • App Type [ Native App]
  • Postman Version [7.2.0]
  • OS: Microsoft Windows [Version 10.0.17763.557]
@abhijitkane
Copy link
Member

@chucklu We are unable to comment on behavior of individual endpoints. Can you send a POST request to https://postman-echo.com/post and check the response?

@chucklu
Copy link
Author

chucklu commented Jul 15, 2019

What did you mean by "behavior of individual endpoints"?

I tried send a post request as following

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

and the response is

{
"args": {},
"data": {},
"files": {},
"form": {},
"headers": {
"x-forwarded-proto": "https",
"host": "postman-echo.com",
"content-length": "0",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.17763; en-US) PowerShell/6.2.0",
"via": "HTTPS/1.1 sasygsha01prx11.asnet.accorservices.net AC10D21F",
"x-forwarded-port": "443"
},
"json": null,
"url": "https://postman-echo.com/post"
}

What's the relationship with the problem I encountered and the new test for https://postman-echo.com/post?

@abhijitkane
Copy link
Member

@chucklu We can only investigate discrepancy in sending the request. https://postman-echo.com/post will return whatever you send in the request. It should return all URL parameters, request headers/body parameters that you send.

We'll be unable to investigate why the behavior of hdgateway.zto.com is different when making the request from the Powershell snippet.

@chucklu
Copy link
Author

chucklu commented Jul 15, 2019

@abhijitkane I remove the | ConvertTo-Json code from last line of the Powershell code

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", 'text/html')
$headers.Add("Content-Type", 'application/x-www-form-urlencoded')

$body = "billCode=75132077503374"

$response = Invoke-RestMethod 'https://hdgateway.zto.com/WayBill_GetDetail' -Method 'POST' -Headers $headers -Body $body
$response

And the response is as following:

PS C:\Users\clu\Desktop> .\postman.ps1 | Format-List

result : @{prescriptionStatus=5; logisticsRecord=System.Object[]; billPrescription=1天17小时; billCode=75132077503374}
message : 快递信息查询成功
status : True
statusCode :

The logisticsRecord property was an object array.

Then I tried to inspect the request by fiddler, it seems the response was correct. But the response did not get parsed correctly by Powershell.
image

There is no problem with the Powershell code generated by Postman,I will submit an issue in Powershell project later.

@chucklu
Copy link
Author

chucklu commented Jul 16, 2019

@abhijitkane Finally I found the problem could be resolved by the depth parameter of ConvertTo-Json.
Powershell ConvertTo-JSON missing nested level
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-6#parameters
-Depth
Specifies how many levels of contained objects are included in the JSON representation. The default value is 2.

I have a suggestion, you might just return $response, and let the user to convert the json with the desired depth.
For Example, I can save the Powershell code into file postman.ps1

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", 'text/html')
$headers.Add("Content-Type", 'application/x-www-form-urlencoded')

$body = "billCode=75132077503374"

$response = Invoke-RestMethod 'https://hdgateway.zto.com/WayBill_GetDetail' -Method 'POST' -Headers $headers -Body $body
$response

and then execute it with the specific depth

.\postman.ps1 | ConvertTo-Json -Depth 5

@chucklu chucklu reopened this Jul 16, 2019
@chucklu
Copy link
Author

chucklu commented Jul 16, 2019

Also find another stackoverflow summary
Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2
And topic on PowerShell/PowerShell#8381

@DannyDainton
Copy link
Contributor

Closing this issue due to inactivity.

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

No branches or pull requests

4 participants