NodeJS'请求'模块:HTTP POST返回415
我正在使用节点模块“请求”来发出POST请求,甚至在尝试了许多不同的内容types变体之后,例如:
'content-type': 'application/json'
我仍然得到一个415(不支持媒体types)的回应。 以下是我的function:
var createNetworkResource = function (resourceName, inputDirectory) { request.post({url:browser.params.baseRestUrl + 'resources/connections', auth: browser.params.auth, form: {method:'POST', headers: {'content-type': 'application/json'}, displayName: "RESOURCE_TEST", name: "RESOURCE_TEST", type: "NETWORK", }}, function(e,r,user){ console.log("Status code of createNetworkResource('" + resourceName + "'): " + r.statusCode);}); };
我已经尝试了从stackoverflow不同的解决scheme,但没有一个工作,我找不到一个特定于此节点模块。
request
的form
属性将内容types设置为application/x-www-form-urlencoded
。
使用json: true
并指定一个body: {}
将添加一个Content-type: application/json
头,并将请求转换为一个JSONstring。
function createNetworkResource (resourceName, inputDirectory) { request.post({ url: browser.params.baseRestUrl + 'resources/connections', auth: browser.params.auth, json: true, body: { displayName: "RESOURCE_TEST", name: "RESOURCE_TEST", type: "NETWORK" } }, function(error, response, user){ if (error) return console.error('error(%s):', resourceName, error) console.log("Status code of createNetworkResource(%s): %s", resourceName, response.statusCode) } ); };