作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过调用 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":[
{
"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;
我面临两个问题,
无论哪种情况,我得到的状态代码都是 200,而不是 201,这是根据 shopify 文档我应该收到的。
有人可以帮我解决问题吗?
编辑:使用 Post,我收到类型错误
const https = require('https');
var data = JSON.stringify(obj)
var options = {
hostname: 'https://{apikey:password}@tuscstore.myshopify.com/admin/api/2020-01',
path: '/products.json',
method: 'POST',
headers: {
'Content-Type': 'application/json',
/*'Content-Length': data.length*/
'Authorization' : 'API_TOKEN'
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
类型错误:无法在“窗口”上执行“获取”:无法解析 https://[https:// 中的 URL {APIKEY:PWD}@tuscstore.myshopify.com/admin/api/2020-01]/products.json
最佳答案
您创建一个新产品时,您必须发出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'
}
}
或者您可以使用此软件包来解决您的所有问题 https://www.npmjs.com/package/shopify-api-node
关于javascript - 通过调用shopify api创建新产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60256456/
您好,我正在与 bigcommerce 合作,我希望在每个类别页面上获得以下风格的分割 本质上是制作类别版本; %%Panel.HomeFeaturedProducts%% %%Panel.SideT
我是一名优秀的程序员,十分优秀!