cURL to PowerShell - Hash Table
So, after posting my earlier cURL to PowerShell Hash Table question, (which was solved), I've now run into a further issue converting cURL to PowerShell this time with 3 hash tables in data(?) (or more likely my PowerShell skills). This time, my script via PowerShell returns:
...General Error java.util.LinkedHashMap cannot be cast to java.util.List
Here is the cURL which works perfectly via Postman:
curl -X PATCH
https://example.com/apis/v1.1/parameters
-H 'Authorization: Bearer 1234567890'
-H 'Content-Type: application/json'
-H 'cache-control: no-cache'
-d '{
"data": [
{
"DUID": 3299,
"AID": 551,
"CID": 10002,
"Parameters": [
{
"name": "Customer.1/AddressLine1",
"value": "SOMEWHERE ROAD"
}
]
}
]
}'
Here is the PowerShell script, which I constructed using advice given in my earlier question here:
cURL to PowerShell - Double hash table in --data?
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
'data'= @{
'DUID'= 3299;
'AID'= 551;
'CID'= 10002;
'Parameters'=
@{'name'= "Customer.1/AddressLine1";
'value'= "SOMEWHERE ROAD"}
}
}
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json) -replace '"', '"')
& $CURLEXE @CurlArgument
My $CurlArgument looks like this:
-X
PATCH
https://example.com/apis/v1.1/parameters
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-H
cache-control: no-cache
-d
{
"data": {
"CID": 10002,
"DUID": 3299,
"AID": 551,
"Parameters": {
"value": "SOMEWHERE ROAD",
"name": "Customer.1/AddressLine1"
}
}
}
Which returns this error:
{"status":"FAILURE","errors":[{"code":"5004","name":"General Error","severity":"3","message":"Error Occured During the operation","details":{"5004":"General Error java.util.LinkedHashMap cannot be cast to java.util.List"}}]}
Could it be the forward slash in the 'Customer.1/AddressLine1' field? I tried a second -replace with these and still got the same error:
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '/')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '2f')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '%2f')
Could it be the lack of square brackets in data hash table? They are there in cURL but not in PowerShell, however in my previous script I didn't have square brackets, plus PowerShell doesn't seem to like them.
Could it be the order of "value" and "name" being changed by PowerShell?
Any assistance would be greatly appreciated.
Update
Thanks to the help so far from @mklement0, I've edited my PowerShell to the following which resolved it!
$Body = @{
data = , @{
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
$CurlArgument = '-X', 'PATCH',
'https://example.com/apis/v1.1/parameters',
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
json powershell curl hashtable
add a comment |
So, after posting my earlier cURL to PowerShell Hash Table question, (which was solved), I've now run into a further issue converting cURL to PowerShell this time with 3 hash tables in data(?) (or more likely my PowerShell skills). This time, my script via PowerShell returns:
...General Error java.util.LinkedHashMap cannot be cast to java.util.List
Here is the cURL which works perfectly via Postman:
curl -X PATCH
https://example.com/apis/v1.1/parameters
-H 'Authorization: Bearer 1234567890'
-H 'Content-Type: application/json'
-H 'cache-control: no-cache'
-d '{
"data": [
{
"DUID": 3299,
"AID": 551,
"CID": 10002,
"Parameters": [
{
"name": "Customer.1/AddressLine1",
"value": "SOMEWHERE ROAD"
}
]
}
]
}'
Here is the PowerShell script, which I constructed using advice given in my earlier question here:
cURL to PowerShell - Double hash table in --data?
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
'data'= @{
'DUID'= 3299;
'AID'= 551;
'CID'= 10002;
'Parameters'=
@{'name'= "Customer.1/AddressLine1";
'value'= "SOMEWHERE ROAD"}
}
}
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json) -replace '"', '"')
& $CURLEXE @CurlArgument
My $CurlArgument looks like this:
-X
PATCH
https://example.com/apis/v1.1/parameters
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-H
cache-control: no-cache
-d
{
"data": {
"CID": 10002,
"DUID": 3299,
"AID": 551,
"Parameters": {
"value": "SOMEWHERE ROAD",
"name": "Customer.1/AddressLine1"
}
}
}
Which returns this error:
{"status":"FAILURE","errors":[{"code":"5004","name":"General Error","severity":"3","message":"Error Occured During the operation","details":{"5004":"General Error java.util.LinkedHashMap cannot be cast to java.util.List"}}]}
Could it be the forward slash in the 'Customer.1/AddressLine1' field? I tried a second -replace with these and still got the same error:
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '/')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '2f')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '%2f')
Could it be the lack of square brackets in data hash table? They are there in cURL but not in PowerShell, however in my previous script I didn't have square brackets, plus PowerShell doesn't seem to like them.
Could it be the order of "value" and "name" being changed by PowerShell?
Any assistance would be greatly appreciated.
Update
Thanks to the help so far from @mklement0, I've edited my PowerShell to the following which resolved it!
$Body = @{
data = , @{
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
$CurlArgument = '-X', 'PATCH',
'https://example.com/apis/v1.1/parameters',
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
json powershell curl hashtable
2
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with@{}
instead of with@()
.
– Lee_Dailey
Nov 25 '18 at 3:35
add a comment |
So, after posting my earlier cURL to PowerShell Hash Table question, (which was solved), I've now run into a further issue converting cURL to PowerShell this time with 3 hash tables in data(?) (or more likely my PowerShell skills). This time, my script via PowerShell returns:
...General Error java.util.LinkedHashMap cannot be cast to java.util.List
Here is the cURL which works perfectly via Postman:
curl -X PATCH
https://example.com/apis/v1.1/parameters
-H 'Authorization: Bearer 1234567890'
-H 'Content-Type: application/json'
-H 'cache-control: no-cache'
-d '{
"data": [
{
"DUID": 3299,
"AID": 551,
"CID": 10002,
"Parameters": [
{
"name": "Customer.1/AddressLine1",
"value": "SOMEWHERE ROAD"
}
]
}
]
}'
Here is the PowerShell script, which I constructed using advice given in my earlier question here:
cURL to PowerShell - Double hash table in --data?
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
'data'= @{
'DUID'= 3299;
'AID'= 551;
'CID'= 10002;
'Parameters'=
@{'name'= "Customer.1/AddressLine1";
'value'= "SOMEWHERE ROAD"}
}
}
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json) -replace '"', '"')
& $CURLEXE @CurlArgument
My $CurlArgument looks like this:
-X
PATCH
https://example.com/apis/v1.1/parameters
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-H
cache-control: no-cache
-d
{
"data": {
"CID": 10002,
"DUID": 3299,
"AID": 551,
"Parameters": {
"value": "SOMEWHERE ROAD",
"name": "Customer.1/AddressLine1"
}
}
}
Which returns this error:
{"status":"FAILURE","errors":[{"code":"5004","name":"General Error","severity":"3","message":"Error Occured During the operation","details":{"5004":"General Error java.util.LinkedHashMap cannot be cast to java.util.List"}}]}
Could it be the forward slash in the 'Customer.1/AddressLine1' field? I tried a second -replace with these and still got the same error:
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '/')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '2f')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '%2f')
Could it be the lack of square brackets in data hash table? They are there in cURL but not in PowerShell, however in my previous script I didn't have square brackets, plus PowerShell doesn't seem to like them.
Could it be the order of "value" and "name" being changed by PowerShell?
Any assistance would be greatly appreciated.
Update
Thanks to the help so far from @mklement0, I've edited my PowerShell to the following which resolved it!
$Body = @{
data = , @{
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
$CurlArgument = '-X', 'PATCH',
'https://example.com/apis/v1.1/parameters',
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
json powershell curl hashtable
So, after posting my earlier cURL to PowerShell Hash Table question, (which was solved), I've now run into a further issue converting cURL to PowerShell this time with 3 hash tables in data(?) (or more likely my PowerShell skills). This time, my script via PowerShell returns:
...General Error java.util.LinkedHashMap cannot be cast to java.util.List
Here is the cURL which works perfectly via Postman:
curl -X PATCH
https://example.com/apis/v1.1/parameters
-H 'Authorization: Bearer 1234567890'
-H 'Content-Type: application/json'
-H 'cache-control: no-cache'
-d '{
"data": [
{
"DUID": 3299,
"AID": 551,
"CID": 10002,
"Parameters": [
{
"name": "Customer.1/AddressLine1",
"value": "SOMEWHERE ROAD"
}
]
}
]
}'
Here is the PowerShell script, which I constructed using advice given in my earlier question here:
cURL to PowerShell - Double hash table in --data?
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
'data'= @{
'DUID'= 3299;
'AID'= 551;
'CID'= 10002;
'Parameters'=
@{'name'= "Customer.1/AddressLine1";
'value'= "SOMEWHERE ROAD"}
}
}
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json) -replace '"', '"')
& $CURLEXE @CurlArgument
My $CurlArgument looks like this:
-X
PATCH
https://example.com/apis/v1.1/parameters
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-H
cache-control: no-cache
-d
{
"data": {
"CID": 10002,
"DUID": 3299,
"AID": 551,
"Parameters": {
"value": "SOMEWHERE ROAD",
"name": "Customer.1/AddressLine1"
}
}
}
Which returns this error:
{"status":"FAILURE","errors":[{"code":"5004","name":"General Error","severity":"3","message":"Error Occured During the operation","details":{"5004":"General Error java.util.LinkedHashMap cannot be cast to java.util.List"}}]}
Could it be the forward slash in the 'Customer.1/AddressLine1' field? I tried a second -replace with these and still got the same error:
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '/')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '2f')
(($Body | ConvertTo-Json) -replace '"', '"' -replace '/', '%2f')
Could it be the lack of square brackets in data hash table? They are there in cURL but not in PowerShell, however in my previous script I didn't have square brackets, plus PowerShell doesn't seem to like them.
Could it be the order of "value" and "name" being changed by PowerShell?
Any assistance would be greatly appreciated.
Update
Thanks to the help so far from @mklement0, I've edited my PowerShell to the following which resolved it!
$Body = @{
data = , @{
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
$CurlArgument = '-X', 'PATCH',
'https://example.com/apis/v1.1/parameters',
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
json powershell curl hashtable
json powershell curl hashtable
edited Nov 26 '18 at 3:48
Simon
asked Nov 25 '18 at 0:55
SimonSimon
506
506
2
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with@{}
instead of with@()
.
– Lee_Dailey
Nov 25 '18 at 3:35
add a comment |
2
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with@{}
instead of with@()
.
– Lee_Dailey
Nov 25 '18 at 3:35
2
2
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with
@{}
instead of with @()
.– Lee_Dailey
Nov 25 '18 at 3:35
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with
@{}
instead of with @()
.– Lee_Dailey
Nov 25 '18 at 3:35
add a comment |
1 Answer
1
active
oldest
votes
Lee Daily provided the crucial pointer:
Your JSON format requires an array of sub-objects ([...]
) as the value of the data
and data.Parameters
properties, whereas the seemingly equivalent hashtables you've constructed in $Body
have only a single, scalar sub-object.
Two tweaks to your code are required:
Ensure that
data
anddata.Parameters
contain arrays:
- Use
, <object>
to construct a single-element array containing<object>
;ConvertTo-Json
automatically converts arrays to JSON[ ... ]
notation.
- Use
Use
-Depth 4
withConvertTo-Json
so as to ensure that all sub-objects in the object hierarchy are represented in full.
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
data = , @{ # Note the "," to construct an *array*
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{ # Note the "," to construct an *array*
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,Get-Help ConvertTo-Json
), but also about a variety of conceptual topics:Get-Help about_Hash_Tables
,Get-Help about_Splatting
. While there isGet-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.
– mklement0
Nov 26 '18 at 2:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463771%2fcurl-to-powershell-hash-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Lee Daily provided the crucial pointer:
Your JSON format requires an array of sub-objects ([...]
) as the value of the data
and data.Parameters
properties, whereas the seemingly equivalent hashtables you've constructed in $Body
have only a single, scalar sub-object.
Two tweaks to your code are required:
Ensure that
data
anddata.Parameters
contain arrays:
- Use
, <object>
to construct a single-element array containing<object>
;ConvertTo-Json
automatically converts arrays to JSON[ ... ]
notation.
- Use
Use
-Depth 4
withConvertTo-Json
so as to ensure that all sub-objects in the object hierarchy are represented in full.
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
data = , @{ # Note the "," to construct an *array*
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{ # Note the "," to construct an *array*
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,Get-Help ConvertTo-Json
), but also about a variety of conceptual topics:Get-Help about_Hash_Tables
,Get-Help about_Splatting
. While there isGet-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.
– mklement0
Nov 26 '18 at 2:11
add a comment |
Lee Daily provided the crucial pointer:
Your JSON format requires an array of sub-objects ([...]
) as the value of the data
and data.Parameters
properties, whereas the seemingly equivalent hashtables you've constructed in $Body
have only a single, scalar sub-object.
Two tweaks to your code are required:
Ensure that
data
anddata.Parameters
contain arrays:
- Use
, <object>
to construct a single-element array containing<object>
;ConvertTo-Json
automatically converts arrays to JSON[ ... ]
notation.
- Use
Use
-Depth 4
withConvertTo-Json
so as to ensure that all sub-objects in the object hierarchy are represented in full.
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
data = , @{ # Note the "," to construct an *array*
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{ # Note the "," to construct an *array*
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,Get-Help ConvertTo-Json
), but also about a variety of conceptual topics:Get-Help about_Hash_Tables
,Get-Help about_Splatting
. While there isGet-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.
– mklement0
Nov 26 '18 at 2:11
add a comment |
Lee Daily provided the crucial pointer:
Your JSON format requires an array of sub-objects ([...]
) as the value of the data
and data.Parameters
properties, whereas the seemingly equivalent hashtables you've constructed in $Body
have only a single, scalar sub-object.
Two tweaks to your code are required:
Ensure that
data
anddata.Parameters
contain arrays:
- Use
, <object>
to construct a single-element array containing<object>
;ConvertTo-Json
automatically converts arrays to JSON[ ... ]
notation.
- Use
Use
-Depth 4
withConvertTo-Json
so as to ensure that all sub-objects in the object hierarchy are represented in full.
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
data = , @{ # Note the "," to construct an *array*
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{ # Note the "," to construct an *array*
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
Lee Daily provided the crucial pointer:
Your JSON format requires an array of sub-objects ([...]
) as the value of the data
and data.Parameters
properties, whereas the seemingly equivalent hashtables you've constructed in $Body
have only a single, scalar sub-object.
Two tweaks to your code are required:
Ensure that
data
anddata.Parameters
contain arrays:
- Use
, <object>
to construct a single-element array containing<object>
;ConvertTo-Json
automatically converts arrays to JSON[ ... ]
notation.
- Use
Use
-Depth 4
withConvertTo-Json
so as to ensure that all sub-objects in the object hierarchy are represented in full.
$CURLEXE = 'C:WindowsSystem32curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"
$Body = @{
data = , @{ # Note the "," to construct an *array*
DUID = 3299
AID = 551
CID = 10002
Parameters = , @{ # Note the "," to construct an *array*
name = "Customer.1/AddressLine1"
value = "SOMEWHERE ROAD"
}
}
}
# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
$URL1,
'-H',
$AuthBearer,
'-H', 'Content-Type: application/json',
'-H', 'cache-control: no-cache',
'-d',
(($Body | ConvertTo-Json -Depth 4) -replace '"', '"')
& $CURLEXE @CurlArgument
edited Nov 25 '18 at 22:59
answered Nov 25 '18 at 15:50
mklement0mklement0
134k21249287
134k21249287
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,Get-Help ConvertTo-Json
), but also about a variety of conceptual topics:Get-Help about_Hash_Tables
,Get-Help about_Splatting
. While there isGet-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.
– mklement0
Nov 26 '18 at 2:11
add a comment |
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,Get-Help ConvertTo-Json
), but also about a variety of conceptual topics:Get-Help about_Hash_Tables
,Get-Help about_Splatting
. While there isGet-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.
– mklement0
Nov 26 '18 at 2:11
1
1
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
Thank you SO much @mklement0 - I had one missing comma after my URL which meant my $AuthBearer wasn't being sent, however your suggested amendments above fixed my problem! Out of interest, where is all this documented so I can do some reading/learning?
– Simon
Nov 26 '18 at 0:18
1
1
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,
Get-Help ConvertTo-Json
), but also about a variety of conceptual topics: Get-Help about_Hash_Tables
, Get-Help about_Splatting
. While there is Get-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.– mklement0
Nov 26 '18 at 2:11
Glad to hear it, @Simon. PowerShell has help topics not just for specific cmdlets (e.g.,
Get-Help ConvertTo-Json
), but also about a variety of conceptual topics: Get-Help about_Hash_Tables
, Get-Help about_Splatting
. While there is Get-Help about_Quoting_Rules
, it doesn't cover the additional requirements for calling external programs; you can learn about them here.– mklement0
Nov 26 '18 at 2:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463771%2fcurl-to-powershell-hash-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
your body in the 1st section has a JSON array in brackets. the 2nd one is NOT an array, from what i understand of JSON - it's a hashtable. that is what you defined in the PoSh version with
@{}
instead of with@()
.– Lee_Dailey
Nov 25 '18 at 3:35