如何使用 nodejs 请求模块进行此 curl 调用
How do I make this curl call with nodejs request module
我可以像这样使用 curl 向 browsermob 发送请求:
curl -X PUT -d "pageRef=car" localhost:9000/proxy/8081/har/pageRef
。但是,我想使用请求模块在 nodejs 中执行此操作。使用请求模块的等效命令是什么?我认为它类似于 request.put(url, data, callback)
但是当我尝试 request.put('localhost:9000/proxy/8081/har/pageRef', {form: { pageRef: encodeURI(browser.platform) + encodeURI(browser.browserName) + encodeURI(browser.version) }})
时,我得到
Error: Invalid protocol
at Request.self._buildRequest (/Users/dragonite/Pixel/node_modules/request/request.js:336:53)
at Request.init (/Users/dragonite/Pixel/node_modules/request/request.js:503:10)
at new Request (/Users/dragonite/Pixel/node_modules/request/request.js:97:8)
at request (/Users/dragonite/Pixel/node_modules/request/index.js:50:11)
at Function.request.put (/Users/dragonite/Pixel/node_modules/request/index.js:121:27)
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:203:10)
看起来您只是缺少 url 前面的协议,例如:
request.put('http://localhost:9000/proxy/8081/har/pageRef', {form: { pageRef: encodeURI(browser.platform) + encodeURI(browser.browserName) + encodeURI(browser.version) }})
我可以像这样使用 curl 向 browsermob 发送请求:
curl -X PUT -d "pageRef=car" localhost:9000/proxy/8081/har/pageRef
。但是,我想使用请求模块在 nodejs 中执行此操作。使用请求模块的等效命令是什么?我认为它类似于 request.put(url, data, callback)
但是当我尝试 request.put('localhost:9000/proxy/8081/har/pageRef', {form: { pageRef: encodeURI(browser.platform) + encodeURI(browser.browserName) + encodeURI(browser.version) }})
时,我得到
Error: Invalid protocol
at Request.self._buildRequest (/Users/dragonite/Pixel/node_modules/request/request.js:336:53)
at Request.init (/Users/dragonite/Pixel/node_modules/request/request.js:503:10)
at new Request (/Users/dragonite/Pixel/node_modules/request/request.js:97:8)
at request (/Users/dragonite/Pixel/node_modules/request/index.js:50:11)
at Function.request.put (/Users/dragonite/Pixel/node_modules/request/index.js:121:27)
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:203:10)
看起来您只是缺少 url 前面的协议,例如:
request.put('http://localhost:9000/proxy/8081/har/pageRef', {form: { pageRef: encodeURI(browser.platform) + encodeURI(browser.browserName) + encodeURI(browser.version) }})