curl -F 'data=@./resourcesByFileId.png' -F 'props={"title":"resource title"}' http://localhost:27583/resources
async create(fd: FormData) {
const resp = await axios.post<ResourceGetRes>(
ApiUtil.baseUrl('/resources'),
fd,
{
headers: fd.getHeaders(),
},
)
return resp.data
}
const fd = new FormData()
const path = resolve(__dirname, '../resource/resourcesByFileId.png')
console.log('test create: ', path)
fd.append('props', JSON.stringify({ title: '图片标题' }))
fd.append('data', createReadStream(path))
await resourceApi.create(fd)
I tried it with form-data and axios and it blew up
Original question https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/3?u=rxliuli
I tried fetch and it succeeded immediately. Is there any difference between the two below?
describe('diff fetch and axios', () => {
function getFormData() {
const fd = new FormData()
const path = resolve(__dirname, '../resource/resourcesByFileId.png')
const title = 'image title'
fd.append('props', JSON.stringify({ title: title }))
fd.append('data', createReadStream(path))
return fd
}
it('test create by fetch', async () => {
const fd = getFormData()
const resp = await fetch(ApiUtil.baseUrl('/resources'), {
method: 'post',
body: fd,
})
const json = await resp.json()
console.log('json: ', json)
})
it.skip('test create by axios', async () => {
const fd = getFormData()
const resp = await axios.post(ApiUtil.baseUrl('/resources'), fd, {
headers: fd.getHeaders(),
})
console.log('resp.data: ', resp.data)
})
})