微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node/Express - PowerShell:Invoke-WebRequest:找不到接受参数“内容类型:应用程序/json”的位置参数

如何解决Node/Express - PowerShell:Invoke-WebRequest:找不到接受参数“内容类型:应用程序/json”的位置参数

我写了一个示例 REST 代码,我在下面分享

const express = require('express');
const app = express();
app.use(express.json());

app.listen(3000,() => console.log('Listening to port 3000'));

app.post('/hello',(req,res)=>{
    console.log('Headers : ',req.headers);
    console.log('Method : ',req.method);
    console.log('Body : ',req.body);
    res.send('Received POST Request ..!!\n');
});

我正在尝试使用以下 curl 命令进行 POST 调用

curl --header '内容类型:应用程序/json' http://localhost:3000/hello --data '{"foo": "bar"}'

尝试使用 curl 访问 POST 时出现以下错误

Error Screen

解决方法

在我的 Powershell 上,curl 实际上是 Invoke WebRequest 的别名。我认为它与 curl 有所不同。

您可以通过运行以下命令来验证它:import java.util.*; public class OccurrenceOfChar{ public static void main (String[] args) { Scanner in = new Scanner(System.in); int n[] = new int[]{0,0},flag=0; System.out.println("Enter a word:"); String input = in.nextLine(); input=input.toLowerCase(); for(int i=0;i<input.length();i++){ if(((int)input.charAt(i)-97)<0 || ((int)input.charAt(i)-97)>25){ flag=1; } else{ n[(int)input.charAt(i)-97]++; } } if(flag==1){ System.out.println("Not a valid string"); } else{ System.out.println("Enter the character:"); String t = in.nextLine(); String s=t.toLowerCase(); if(((int)s.charAt(0)-97)<0 || ((int)s.charAt(0)-97)>25){ System.out.println("Given character is not an alphabet"); } else{ if(n[(int)s.charAt(0)-97]>0){ System.out.println("No of\'"+s+"\' present in the given word is "+n[(int)s.charAt(0)-97]+"."); } else{ System.out.println("The given character \'"+s+"\' not present in the given word."); } } } } 。如果是这样,那么您可以运行

Get-Alias | grep curl

然后重试您的命令

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。