2

I developed a bot to scrape some info via "API" in Python and now I want to change the stack to Powershell.

I'm having difficulties with one of the requests. In Python it works like a charm but on Powershell I'm unable to make it work in the same way.

In Python, I just create the payload

  arrayWebCodes = ["PR1234", "PR3456"]
  payloadStock={
        "productsCN[]": arrayWebCodes ,
        "productsCC[]": arrayWebCodes 
    }
  response = s.post(urlStock, data=payloadStock)

This is because I want to obtain info for multiples products in a single call.

When I try to implement same on Powershell, response is empty.

$arrayProducts = @("PR1234", "PR73456")
$payloadStock = [ordered]@{
    "productsCN[]" = $arrayProducts
    "productsCC[]" = $arrayProducts
}
$response = Invoke-WebRequest -Uri $urlStock -body $payloadStock -Method "POST" -WebSession $webSession

Somehow, Powershell Hashtables of Arrays have some differences with Python dictionary of arrays I'm trying to implement here.

If I implement the request with only one code in Powershell, it works. So if I go from:

$codProduct1 = "PR1234"
$codProduct2 = "PR3456"
$payloadStock = [ordered]@{
    "productsCN[]" = $codProduct1, $codProduct2
    "productsCC[]" = $codProduct1, $codProduct2
}
$response = Invoke-WebRequest -Uri $urlStock -body $payloadStock -Method "POST" -WebSession $webSession

to:

    $codProduct1 = "PR1234"
    $codProduct2 = "PR3456"
    $payloadStock = [ordered]@{
        "productsCN[]" = $codProduct1
        "productsCC[]" = $codProduct1
    }
    $response = Invoke-WebRequest -Uri $urlStock -body $payloadStock -Method "POST" -WebSession $webSession

It Works...

I'm about to quit, any help is really appreciated.

Daniziz
  • 88
  • 7
  • Change From : [ordered] To: [psobject] – jdweng Aug 01 '23 at 09:47
  • Same behaviour... – Daniziz Aug 01 '23 at 09:55
  • What PowerShell version are you using? (if this is *Windows* PowerShell, try [PowerShell 7](https://learn.microsoft.com/powershell/scripting/install/installing-powershell-on-windows)) – iRon Aug 01 '23 at 10:52
  • $PSVersionTable PSVersion 7.3.6 PSEdition Core – Daniziz Aug 01 '23 at 11:18
  • 1
    Just noticed that you doing a `POST` for Python and a `GET` for PowerShell... – iRon Aug 01 '23 at 11:34
  • Yes! Nice catch sorry for that... unfortunately same outcome! I wish i could share more details... basically Just obtained the data from developer tools in the explorer. I've been playing around with it and it seems it accepts several methods... but sticking to same(POST), still empty... – Daniziz Aug 01 '23 at 12:04
  • 1
    Hi @iRon!!! Changing it to POST and making a change in the URL made it work... THANKS! – Daniziz Aug 01 '23 at 12:13
  • Make items an array : "productsCN[]" = @($codProduct1, $codProduct2) – jdweng Aug 01 '23 at 12:14

1 Answers1

0

Not really the answer to your issue, but just to be sure.
This should be the same:

Python

import json

arrayWebCodes = ["PR1234", "PR3456"]
payloadStock={
    "productsCN[]": arrayWebCodes ,
    "productsCC[]": arrayWebCodes 
}
print(json.dumps(payloadStock))
{"productsCN[]": ["PR1234", "PR3456"], "productsCC[]": ["PR1234", "PR3456"]}

PowerShell

$arrayProducts = @("PR1234", "PR73456")
$payloadStock = [ordered]@{
    "productsCN[]" = $arrayProducts
    "productsCC[]" = $arrayProducts
}
$payloadStock | ConvertTo-Json -Compress
{"productsCN[]":["PR1234","PR73456"],"productsCC[]":["PR1234","PR73456"]}

I suspect that the Invoke-WebRequest -body might have the same -Depth default issue as for unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2.
What happens if you provide the -body as a (json) string:

$jsonPayload = $payloadStock | ConvertTo-Json -Compress -Depth 9
$response = Invoke-WebRequest -Uri $urlStock -body $jsonPayload -Method GET -WebSession $webSession
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Thanks for the help. I also tried that. API does not seems affected by sending it in JSON string or PSObject (Did not tested on Python) Here is the output for the convertto-json (Yes, they are the same...) Also tested in different files and with JSON parser :( {"productsCC[]":["PR723345","PR710663"],"productsCN[]":["PR723345","PR710663"]} – Daniziz Aug 01 '23 at 11:21
  • Related Depth topic: I always try to do all conversions with "-depth 99" for avoiding issues. Anyway, this JSON will always have a depth of 1... but I'll dig on Invoke-WebRequest... – Daniziz Aug 01 '23 at 11:26