Javascript 通过调用shopify api创建新产品
我试图通过调用shopify产品api(/admin/api/2020-01/products.json)来创建一个新产品。我正在尝试使用“https”模块来实现这一点。下面是示例代码Javascript 通过调用shopify api创建新产品,javascript,node.js,reactjs,https,shopify-app,Javascript,Node.js,Reactjs,Https,Shopify App,我试图通过调用shopify产品api(/admin/api/2020-01/products.json)来创建一个新产品。我正在尝试使用“https”模块来实现这一点。下面是示例代码 const url1 = 'https://{api_token}@tuscstore.myshopify.com/admin/api/2020-01/products.json'; var obj = { "product":[ { "titl
const url1 = 'https://{api_token}@tuscstore.myshopify.com/admin/api/2020-01/products.json';
var obj = {
"product":[
{
"title": "Saturn",
"body_html": "<p>The epitome of elegance</p>",
"vendor": "Soltions inc",
"product_type": "Planets",
"handle": "saturn",
"tags": "",
"images": [
{
"src": "https://solarsystem.nasa.gov/system/stellar_items/image_files/38_saturn_1600x900.jpg"
}
]
}
]
};
const https = require('https');
var data = JSON.stringify(obj)
const options = new URL(url1);
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
/* res.on('data', (d) => {
process.stdout.write(d);
}); */
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
const Index = () => (
<div>
<p>Sample app using React and Next.js</p>
</div>
);
export default Index;
TypeError:未能在“窗口”上执行“fetch”:未能解析来自{APIKEY:PWD}@tuscstore.myshopify.com/admin/api/2020-01]/products.json的URL。创建新产品时,必须发出http POST请求,现在发出http GET请求时,应更新
选项,如下所示:
const options = {
hostname: 'https://apikey:password@<@store_url>/admin/api/2020-01', // your host name
path: '/shop.json', // your end point
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization' : 'YOUR_API_TOKEN'
}
}
const选项={
主机名:'https://apikey:password@/admin/api/2020-01',//您的主机名
路径:'/shop.json',//您的终点
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
“授权”:“您的API令牌”
}
}
或者您可以使用此软件包解决所有问题“授权”:“您的\u API\u令牌”,这是我的合作伙伴帐户中的令牌吗?根据文档,它似乎是您的合作伙伴帐户中的令牌
const options = {
hostname: 'https://apikey:password@<@store_url>/admin/api/2020-01', // your host name
path: '/shop.json', // your end point
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization' : 'YOUR_API_TOKEN'
}
}