我使用以下代码使用Curl和Node.JS执行rest API。它正在我的本地机器上工作。
C:\Windows\system32>pip install sasl
Collecting sasl
Using cached sasl-0.2.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): six in c:\python34\lib
\site-packages (from sasl)
Installing collected packages: sasl
Running setup.py install for sasl ... error
Complete output from command C:\Python34\python.exe -u -c "import setuptools
, tokenize;__file__='C:\\Users\\username\\AppData\\Local\\Temp\\pip-build-m7m3weo7
\\sasl\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().
replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\username\AppD
ata\Local\Temp\pip-rz3nmdwp-record\install-record.txt --single-version-externall
y-managed --compile:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.4
creating build\lib.win-amd64-3.4\sasl
copying sasl\__init__.py -> build\lib.win-amd64-3.4\sasl
running egg_info
writing sasl.egg-info\PKG-INFO
writing requirements to sasl.egg-info\requires.txt
writing top-level names to sasl.egg-info\top_level.txt
writing dependency_links to sasl.egg-info\dependency_links.txt
warning: manifest_maker: standard file '-c' not found
reading manifest file 'sasl.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'sasl.egg-info\SOURCES.txt'
copying sasl\saslwrapper.cpp -> build\lib.win-amd64-3.4\sasl
copying sasl\saslwrapper.h -> build\lib.win-amd64-3.4\sasl
copying sasl\saslwrapper.pyx -> build\lib.win-amd64-3.4\sasl
running build_ext
building 'sasl.saslwrapper' extension
creating build\temp.win-amd64-3.4
creating build\temp.win-amd64-3.4\Release
creating build\temp.win-amd64-3.4\Release\sasl
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\x86_amd64\cl.exe
/c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Isasl -IC:\Python34\include -IC:\Python34\
include /Tpsasl/saslwrapper.cpp /Fobuild\temp.win-amd64-3.4\Release\sasl/saslwra
pper.obj
saslwrapper.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Include\xlocale(323)
: warning C4530: C++ exception handler used, but unwind semantics are not enable
d. Specify /EHsc
c:\users\username\appdata\local\temp\pip-build-m7m3weo7\sasl\sasl\saslwrapper.
h(22) : fatal error C1083: Cannot open include file: 'sasl/sasl.h': No such file
or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\B
in\\x86_amd64\\cl.exe' failed with exit status 2
但上传到服务器后,我收到以下错误
...
...
var child_process = require('child_process');
function runCmd(cmd)
{
var resp = child_process.execSync(cmd);
var result = resp.toString('UTF8');
return result;
}
var cmd = "curl -u userx:passx -X POST --data @test.json -H 'Content-Type: application/json' https://mysite.atlassian.net/rest/api/2/issue/";
var result = runCmd(cmd);
...
...
答案 0 :(得分:2)
您的服务器不支持CURL。如果您使用请求库https://github.com/request/request
,情况会更好var request = require('request');
var jsonData;
fs = require('fs')
fs.readFile('test.json', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
jsonData = JSON.parse(data);
});
request({
url: 'https://mysite.atlassian.net/rest/api/2/issue/',
'auth': {
'user': 'userx',
'pass': 'passx',
'sendImmediately': false
},
qs: jsonData,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
答案 1 :(得分:2)
var request = require('request');
request('http://www.httpbin.org', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
})
如何使用node-libcurl
的示例var Curl = require('node-libcurl').Curl;
var curl = new Curl();
curl.setOpt( Curl.option.URL, 'http://www.httpbin.org');
curl.setOpt( Curl.option.FOLLOWLOCATION, true );
curl.setOpt( Curl.option.HTTPPOST, [
{ name: 'login', contents: 'username' }
]);
curl.perform();