我有一个方法可以返回来自另一个服务器的响应。我使用request-promise并将网址放在options
对象中。
正如您在下面的代码中看到的,一切都很好,但是当我发送请求时,它返回404 - resource not found
。
当我用request("https://api.quickpay.net/payments")
修改request(options)
方法时,我从服务器得到了肯定的回答--它告诉我添加头部等等,这是肯定的。
public requestNewQuickpayPayment(order_id: String, currency : String, callback: Function) {
var options = {
method: 'POST',
uri: 'https://api.quickpay.net/payments',
form:{
order_id : "order123",
currency : "dkk"
},
headers: {
"Content-Type" : "application/json",
'Accept-Version': 'v10'
},
json: true
};
request(options).then((response:any)=>{
console.log(response);
return response;
}).catch((error:any)=>{
console.log(error);
return error;
}).finally(()=>{
console.log("done");
})
}
来自控制台的内容
Request {
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
method: 'POST',
uri: [Url],
transform2xxOnly: true,
headers: [Object],
readable: true,
writable: true,
explicitMethod: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Tunnel],
_rp_resolve: [Function],
_rp_reject: [Function],
_rp_promise: [Promise],
_rp_callbackOrig: undefined,
callback: [Function],
_rp_options: [Object],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function: RP$callback],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'api.quickpay.net',
body: 'order_id=asdasdasd¤cy=dkk',
path: '/payments',
_json: true,
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://api.quickpay.net/payments',
req: [ClientRequest],
ntick: true,
response: [Circular],
originalHost: 'api.quickpay.net',
originalHostHeaderName: 'host',
responseContent: [Circular],
_destdata: true,
_ended: true,
_callbackCalled: true },
toJSON: [Function: responseToJSON],
caseless: Caseless { dict: [Object] },
body: '404 Not Found' } }
这里出了什么问题?到资源源的路径被检查了很多次-那里没有任何错误...
发布于 2019-06-06 14:24:19
对于api.quickpay.net
,404 Not Found
并不意味着无法识别URI,而是表示无效的请求正文。它与URI是在options
对象中声明,还是作为request()
的字符串参数声明无关。
这是一个简单的实验。下面的代码将返回“肯定”结果,警告缺少标头({"error":"Accept-Version http header is required"}
),这表明URI被“识别”:
request({
method: 'POST',
uri: 'https://api.quickpay.net/payments'
}, function(err, res, body) {
console.log(body);
});
但是,在添加了缺少的Accept-Version
标头之后,我们得到404 Not Found
request({
method: 'POST',
uri: 'https://api.quickpay.net/payments',
headers: {
'Accept-Version': 'v10'
}
}, function(err, res, body) {
console.log(body);
});
因此,为了使API调用有效,您需要使HTTP请求有效(以下是document)。
https://stackoverflow.com/questions/56404622
复制