等效于nodejs的curl命令 [英] curl command equivalent in nodejs

查看:74
本文介绍了等效于nodejs的curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有curl命令,该命令可以根据需要运行:

I have the curl command here which is working as I want:

curl -X POST -H "Content-Type: application/json" -d @cats.json http://localhost:8080

我想获得将其翻译为nodejs的帮助

And I would like to get help on translating this into nodejs

我目前已设置本地主机,我的目标是显示存储在JSON文件中的数据.我目前将数据复制并粘贴到邮递员中,然后将数据邮寄到本地主机,但是我不想使用邮递员.curl命令很有用,但我想将其合并到我的代码中.

I currently have localhost set up and my goal is to display data stored in a JSON file. I currently copy and paste the data into postman and post the data to the localhost but I want to not use postman. The curl command was useful but I want to incorporate it into my code.

我尝试过

var request = require('request');
request.post({
  headers: {'content-type' : 'application/json'},
  url:     'http://localhost/8080',
  body:    "someData"
}, function(error, response, body){
  console.log(body);
});

我已经尝试过了,但是它提供的结果与curl命令在本地主机中显示数据的结果不同

I've tried this but it doesn't provide the same outcome as the curl command does where the data appears in my localhost

推荐答案

在邮递员中,您可以导出NodeJS请求代码:

In postman, you can export NodeJS request code out:

点击代码然后选择NodeJS-> Request.复制到剪贴板

Click on Code Then select NodeJS -> Request. Copy to Clipboard

这篇关于等效于nodejs的curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆