我尝试使用 Walmart API v4.2 发布一些商品。我使用“批量项目设置”API 方法来创建一些提要。我使用了一些类型的方法来做到这一点:
- 发送带有标头“multipart/form-data”的二进制文件(在请求正文中,for-data)(Walmart API 文档中描述了这种方式)
- 在请求正文中发送带有标头“Content-Type”的字符串化对象:“application/json”,
Walmart API 正确返回我的 feedId。但是所有这些方法都不起作用!提要创建后,我在沃尔玛卖家中心看到了“已提交”状态。但几分钟后,此状态更改为“错误”。在错误列中,我看到“错误类型:数据错误”,描述为“数据格式错误。请检查数据文件以确保其格式正确。java.lang.NullPointerException”。
我使用我的后端 NodeJs 应用程序来做到这一点。我使用 Axios 发出请求。我的代码示例:
async createFeed(wdpId, wdpSecret, accessToken, feedsData) {
try {
const string = JSON.stringify(feedsData);
const file = Buffer.from(string);
const formData = new FormData();
formData.append('file', file);
const baseToken = WalmartService.getBaseAuthToken(wdpId, wdpSecret);
const options = {
params: {
feedType: 'MP_WFS_ITEM',
},
headers: {
Authorization: baseToken,
'WM_SEC.ACCESS_TOKEN': accessToken,
'WM_QOS.CORRELATION_ID': uuidv4(),
'WM_SVC.NAME': 'Walmart Marketplace',
Accept: 'application/json',
'Content-Type': 'application/json',
...formData.getHeaders(),
},
};
return (
axios
.post(`${process.env.WALMART_API_BASEURL}/feeds`, formData, options)
.then((response) => {
return response.data;
})
.catch((error) => {
console.error(error.message);
throw new BadRequestException('Walmart error, ', error.message);
})
);
} catch (error) {
throw new BadRequestException('Can not create listing');
}
}