node.js - node-fetch 将帖子正文作为表单数据发送
问题描述
我正在尝试发送带有正文的 POST 请求,form-data
因为这似乎是唯一可行的方法。
我也在 Postman 中尝试过,但发送body
失败raw JSON
。
所以我尝试做同样的事情,node-fetch
但似乎body
正在发送,JSON
并且我收到与以前相同的错误(raw
从 Postman 使用时)。
try{
const { streamId } = request.body;
const headers = {
"Authorization": INO_AUTHORIZATION_CODE,
// "Content-Type": "multipart/form-data; boundary=<calculated when request is sent>"
"Content-Type": "application/json"
}
const url = `https://www.inoreader.com/reader/api/0/stream/contents/${streamId}`;
const body = {
AppId: INO_APP_ID,
AppKey: INO_APP_KEY
}
const resp = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
// body: body,
headers: headers
});
const json = await resp.text();
return response.send(json);
} catch(error) {
next(error);
}
仅将正文设置为form-data
有效:
解决方案
您需要使用他们的文档中提到的 form-data 包, 以便您的代码
const FormData = require('form-data');
const form = new FormData();
form.append('AppId', INO_APP_ID);
form.append('AppKey', INO_APP_KEY);
const resp = await fetch(url, {
method: 'POST',
body: form
});
推荐阅读
- asp.net - 如何将表单中的值作为参数传递给 Ajax.ActionLink 以调用函数
- google-apps-script - Google App Script将Excel文件的公式转换为#Ref
- java - JDBC Sqlite - 当我通过 Jenkins 运行我的项目时,插入查询不起作用
- android - 如果未选中单选按钮,则阻止提交表单
- rest - 确保微服务中外键/所有权的一致性
- java - FXML 控制器的非 @FXML 实例变量变为空
- mongodb - MongoDB查询聚合函数重新调整空结果
- php - 我将如何使用 php/blade 更改 div 类
- android - 如何从 Call.Callback 和 InCallService TelecomManager 获取呼叫事件?
- jmeter - jmeter,名称和值对在jmeter中是动态的