0

While using the following in powershell its copied from a website to process online payments thro an application. I change the access token and unique key when writing this.

$authHeader = @{ Authorization = 'Bearer  {0}' -f "{{ACCESS_TOKEN}}" }
$body = '{
   "idempotency_key": "{{UNIQUE-KEY}}",
   "autocomplete": true,
   "amount_money": {
     "amount": 100,
     "currency": "USD"
   },
   "source_id": "cnon:card-nonce-ok"
   }
}'

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments |
  -Method Post |
  -ContentType "application/json" |
  -Headers $authHeader |
  -Body $body 

i get the following exception

-Method : The term '-Method' is not recognized as the name of a cmdlet, function, script file, or operable program.
 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  At line:14 char:4   
     +    -Method Post |                                      
 +    ~~~~~~~                                                                                                               
 + CategoryInfo          : ObjectNotFound: (-Method:String) [], CommandNotFoundException                                
 + FullyQualifiedErrorId : CommandNotFoundException   

or if i remove these | and use space instead like comment said,

$authHeader = @{ Authorization = 'Bearer  {0}' -f "{{ACCESS_TOKEN}}" }
$body = '{
   "idempotency_key": "{{UNIQUE-KEY}}",
   "autocomplete": true,
   "amount_money": {
     "amount": 100,
     "currency": "USD"
   },
   "source_id": "cnon:card-nonce-ok"
   }
}'

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments 
  -Method Post 
  -ContentType "application/json" 
  -Headers $authHeader 
  -Body $body 

i get the following exception and as shown the old -Method exception is still there.

Invoke-RestMethod : {"errors": [{"code": "UNAUTHORIZED","detail": "Your request did not include an `Authorization`
http header with an access token. The header value is expected to be of the format \"Bearer TOKEN\"  (without
quotation marks), where TOKEN is to be replaced with your access token  (e.g. \"Bearer {{Access token}}\"). For
more information, see https://docs.connect.squareup.com/api/connect/v2/#requestandresponseheaders. If you are seeing
this error message while using one of our officially supported client libraries, please report this to
[email protected]. ","category": "AUTHENTICATION_ERROR"}]}
At line:13 char:1
+ Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payment ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
-Method : The term '-Method' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:14 char:4
+    -Method Post
+    ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Method:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-ContentType : The term '-ContentType' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:15 char:4
+    -ContentType "application/json"
+    ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ContentType:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:16 char:4
+    -Headers $authHeader
+    ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Headers:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Body : The term '-Body' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:17 char:4
+    -Body $body
+    ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Body:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
2
  • 2
    Do not use the pipe character | to separate your parameters, they should be space separated Commented Feb 4, 2020 at 11:23
  • @arco444 it gave me another error about authorization and still the same error about -Method. Commented Feb 4, 2020 at 11:50

1 Answer 1

2

While the pipe | can be used to break a line into multiple lines, that statement apply only apply for a series of command using the pipeline in the first place.

Parameters tied to a command cannot be separated by the pipeline like that.

You have 3 options.

Put all your parameters on the same line, space separated, like this:

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments -Method Post -ContentType "application/json" -Headers $authHeader -Body $body

Use Splatting and define your parameters into a hashtable, then apply them to the command using @ to specify you are passing a list of parameters and these values and not simply a single hashtable parameter.

$params = @{
    ContentType = "application/json"
    Method = 'Post'
    Body = $body
    Headers = $authHeader
    Uri = 'https://connect.squareupsandbox.com/v2/payments'
}
Invoke-RestMethod @params 

Use the backtick ` character.

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments `
    -Method Post `
    -ContentType "application/json" `
    -Headers $authHeader `
    -Body $body 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.