Home > OS >  -Headers : The term '-Headers' is not recognized as the name of a cmdlet, - Azure function
-Headers : The term '-Headers' is not recognized as the name of a cmdlet, - Azure function

Time:12-24

Thanks for your support and help in advance!

I have an Azure Functions (Stack: .Net - HttpTrigger Template) in VS Code with the below code:

public static class OnPaymentReceived
    {
        [FunctionName("OnPaymentReceived")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Received a payment.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
           
            return new OkObjectResult($"Thank you for your purchase");
        }
    }

I know that we can run the Azure Function in many ways, where I'm using IWR through Windows PowerShell using the below cmdlets:

iwr -Method POST `
-Uri http://localhost:7071/api/OnPaymentReceived
-Headers @{"Content-Type"="application/json"}`
-Body '{}'

I'm getting the successful output but an error related to the 3rd line of cmdlet which is:

Output:

StatusCode        : 200
StatusDescription : OK
Content           : Thank you for your purchase
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: text/plain; charset=utf-8
                    Date: Fri, 24 Dec 2021 01:10:33 GMT
                    Server: Kestrel

                    Thank you for your purchase
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, text/plain; charset=utf-8], [Date, Fri, 24 Dec 2021
                    01:10:33 GMT], [Server, Kestrel]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 27

Exception: When I used the syntax of 3rd cmdlet like: -Headers @{"Content-Type"="application/json"}

-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:3 char:1
  -Headers @{"Content-Type"="application/json"}`
  ~~~~~~~~
      CategoryInfo          : ObjectNotFound: (-Headers:String) [], CommandNotFoundException
      FullyQualifiedErrorId : CommandNotFoundException

When I used the another syntax of 3rd cmdlet like: header('Content-type: application/json');

header : The term 'header' 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:3 char:1
  header('Content-type: application/json');
  ~~~~~~
      CategoryInfo          : ObjectNotFound: (header: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:4 char:1
  -Body '{}'
  ~~~~~
      CategoryInfo          : ObjectNotFound: (-Body:String) [], CommandNotFoundException
      FullyQualifiedErrorId : CommandNotFoundException

Image - [Run Function Output With Exception][1]

How to resolve this exception (What is the right syntax of this -Headers cmdlet) ?

CodePudding user response:

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, function, script file, or operable program.

The error is caused because of the syntax error in the cmdlet.

iwr -Method POST `
-Uri http://localhost:7071/api/OnPaymentReceived `
-headers @{"Content-Type"="application/json"} `
-Body '{} '

After every line of command there must be one space and apostrophe symbol like the below format (-cmdlet cmdends ` ).

I have tried with the above cmdlets , you can see the output without any error.

enter image description here

  • Related