如何将 curl 翻译成 javascript POST 请求
how to translate curl into javascript POST request
我正在尝试连接名为 PayU 的支付 api,他们提供了有关如何通过 curl 连接的示例,一个是通过 rest 连接 api
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-d 'grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f'
还有一个是通过我也想使用的 SDK 连接,但是这个需要在商店中进行额外设置,我在使用第一个时遇到了问题,所以如果有人愿意破译另一个也很棒
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-H "Cache-Control: no-cache"
-H "Content-Type: application/x-www-form-urlencoded"
-d 'grant_type=trusted_merchant&client_id=[provided by PayU]&client_secret=[provided by PayU]&email=[users email]&ext_customer_id=[Id of the customer used in merchant system]'
在 curl 中,第一个交付令牌没有问题,但是我试图在代码中做同样的事情,但我做不到。这是我的代码:
fetch('https://secure.payu.com/pl/standard/user/oauth/authorize', {
method: 'POST',
body: JSON.stringify({
'grant_type': 'client_credentials',
'client_id': '145227',
'client_secret': '12f071174cb7eb79d4aac5bc2f07563f',
})
}).then(res => {
if (!res.ok) {
throw new Error("Fetching payU failed, please try again later!");
}
return res;
})
.then(data => {
console.log(data)
return { payUdata: data }
})
.catch(err => {
console.log(err);
});
基本 Post 正文不是序列化的 json,数据看起来像查询参数,就像在 curl
中一样
{
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: "grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f")
}
来自@BankBuilder 评论:
function querifyObject(obj){
return new URLSearchParams(Object.entries(obj)).toString();
}
然后:
{
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: querifyObject({
'grant_type': 'client_credentials',
'client_id': '145227',
'client_secret': '12f071174cb7eb79d4aac5bc2f07563f',
})
}
我正在尝试连接名为 PayU 的支付 api,他们提供了有关如何通过 curl 连接的示例,一个是通过 rest 连接 api
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-d 'grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f'
还有一个是通过我也想使用的 SDK 连接,但是这个需要在商店中进行额外设置,我在使用第一个时遇到了问题,所以如果有人愿意破译另一个也很棒
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-H "Cache-Control: no-cache"
-H "Content-Type: application/x-www-form-urlencoded"
-d 'grant_type=trusted_merchant&client_id=[provided by PayU]&client_secret=[provided by PayU]&email=[users email]&ext_customer_id=[Id of the customer used in merchant system]'
在 curl 中,第一个交付令牌没有问题,但是我试图在代码中做同样的事情,但我做不到。这是我的代码:
fetch('https://secure.payu.com/pl/standard/user/oauth/authorize', {
method: 'POST',
body: JSON.stringify({
'grant_type': 'client_credentials',
'client_id': '145227',
'client_secret': '12f071174cb7eb79d4aac5bc2f07563f',
})
}).then(res => {
if (!res.ok) {
throw new Error("Fetching payU failed, please try again later!");
}
return res;
})
.then(data => {
console.log(data)
return { payUdata: data }
})
.catch(err => {
console.log(err);
});
基本 Post 正文不是序列化的 json,数据看起来像查询参数,就像在 curl
中一样{
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: "grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f")
}
来自@BankBuilder 评论:
function querifyObject(obj){
return new URLSearchParams(Object.entries(obj)).toString();
}
然后:
{
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: querifyObject({
'grant_type': 'client_credentials',
'client_id': '145227',
'client_secret': '12f071174cb7eb79d4aac5bc2f07563f',
})
}