# How to use joplin api \`/resources/:id/file\`?

**URL:** https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333
**Category:** Development
**Created:** [29 September 2020 01:21 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333 "2020-09-29T01:21:11Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 01:21 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/1 "2020-09-29T01:21:11Z")

</div>

> Original demand: [Feature request: pasting images/files directly · Issue #10 · rxliuli/joplin-vscode-plugin · GitHub](https://github.com/rxliuli/joplin-vscode-plugin/issues/10)  
> Are there any examples available? I want to try to paste the image through vscode and upload it to joplin automatically. Are there any nodejs examples available?

```ts
  static baseUrl(url: string, param?: object) {
    const query = stringify(
      {
        ...param,
        token: config.token,
      },
      {
        arrayFormat: 'comma',
      },
    )
    return `http://localhost:${config.port}${url}?${query}`
  }

  async fileByResourceId(id: string) {
    return (await axios.get(ApiUtil.baseUrl(`/resources/${id}/file`))).data
  }
    const res = await resourceApi.fileByResourceId(id)
    console.log(typeof res)
    const path = resolve(__dirname, '../resource/resourcesByFileId.png')
    writeFileSync(path, res)

```

![image](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/e/e8d51bb0b6fba63929117da3783bca24227142e5.png)  
 ![image](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/b/b1830ea43373faabfc9f027acca1c801c5b7c238.png)

@laurent

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 06:04 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/2 "2020-09-29T06:04:52Z")

</div>

problem solved.

> <https://stackoverflow.com/questions/41846669/download-an-image-using-axios-and-convert-it-to-base64>

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 09:21 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/3 "2020-09-29T09:21:42Z")

</div>

Well, the creation still failed. . . @laurent

Error: Request failed with status code 500

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/d/ddc8e7812de9f2007d4a7ff92692fe0ce5fe29ad.png)

```auto
    const fd = new FormData()
    const path = resolve(__dirname, '../resource/resourcesByFileId.png')
    fd.append('props', JSON.stringify({ title: 'image' }))
    fd.append('data', createReadStream(path))
    const resp = await axios.post<ResourceGetRes>(
      ApiUtil.baseUrl('/resources'),
      fd,
      {
        headers: fd.getHeaders(),
      },
    )
    console.log(resp.data)

```

> <https://github.com/rxliuli/joplin-api/blob/3f3e5d9d3371fab93c1189b35a2926c9aceedcba/test/api/ResourceApi.test.ts#L28>

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [29 September 2020 10:17 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/4 "2020-09-29T10:17:51Z")

</div>

Does it give any error message beside code 500?

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 10:42 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/5 "2020-09-29T10:42:22Z")

</div>

![image](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/a/a555029f60f707325c0c9b73345c7b25bc063a7f.png)

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [29 September 2020 10:53 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/6 "2020-09-29T10:53:47Z")

</div>

I assume the issue is that the request is not sent in multipart format but I can't be sure as I don't know this library.

The curl request would be:

```auto
curl -F 'data=@/path/to/file.jpg' -F 'props={"title":"my resource title"}' http://localhost:41184/resources

```

`-F` means you upload the file as if it was from a form on a web page.

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 11:06 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/7 "2020-09-29T11:06:29Z")

</div>

I tested it

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/1/165e6155a4b51b517a1e6a02b4abcbdf247b40ce.png)

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [29 September 2020 11:16 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/8 "2020-09-29T11:16:07Z")

</div>

With curl it would be `-F 'data=@/path/to/file.jpg'` (see the "@")

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [29 September 2020 13:49 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/9 "2020-09-29T13:49:16Z")

</div>

What is @?

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [29 September 2020 15:31 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/10 "2020-09-29T15:31:28Z")

</div>

This is to make curl load the data from the given file. If you don't put "@" it assumes you want to post the string "/path/to/file.jpg" instead of the file content.

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [30 September 2020 02:00 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/11 "2020-09-30T02:00:32Z")

</div>

I currently create a stackoverflow question: [https://stackoverflow.com/questions/64129884/how-to-convert-the-following-curl-call-to-nodejs-axios](https://stackoverflow.com/questions/64129884/how-to-convert-the-following-curl-call-to-nodejs-axios)

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [30 September 2020 03:51 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/12 "2020-09-30T03:51:24Z")

</div>

Confirmed, there are some problems with axios. . . I use fetch (node-fetch) to upload normally

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [30 September 2020 07:26 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/13 "2020-09-30T07:26:52Z")

</div>

Ok that's good to know. I'd be curious to know how to replicate the 500 error you got though, as the app should have provided a proper error message.

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [30 September 2020 07:31 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/14 "2020-09-30T07:31:41Z")

</div>

Here is an example of axios unit test, you can simply run it here to reproduce

> <https://github.com/rxliuli/joplin-api/blob/7e64ec4a2c2d13c6fda4c1dc8793c29aad4599f2/test/api/ResourceApi.test.ts#L49>

1. clone repo && git checkout dev && yarn install
2. Start electron development client
3. Modify config.token(test/util/setupTestEnv.ts)
4. Remove the skip flag and run it
5. Get this error

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [30 September 2020 07:33 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/15 "2020-09-30T07:33:00Z")

</div>

You should be able to reproduce it with axios anywhere

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [30 September 2020 07:36 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/16 "2020-09-30T07:36:02Z")

</div>

Thanks for the information, I'll give it a try.

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [9 October 2020 09:56 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/17 "2020-10-09T09:56:47Z")

</div>

I was able to replicate the issue but there's no easy way to improve the error code. Technically it should be error 4xx since the request was invalid but since for certain cases it's hard to tell if it's an internal error or user error, an error 5xx is returned.

In general when testing the API, if you get a 500 error, the best is to look at the response body, which should contain detailed info about the error. For example:

> $ curl --data '{"title":"bla"}' [http://localhost:27583/tags?token=TOKEN](http://localhost:27583/tags?token=TOKEN)
> 
> {"title":"bla","id":"35f4e2b755444e35bf2c9b34fff88936","updated\_time":1602237094307,"created\_time":1602237094307,"user\_updated\_time":1602237094307,"user\_created\_time":1602237094307,"type\_":5}

> $ curl --data '{"title":"bla"}' [http://localhost:27583/tags?token=TOKEN](http://localhost:27583/tags?token=TOKEN)
> 
> {"error":"Internal Server Error: The tag "bla" already exists. Please choose a different name.: \n\nError: The tag "bla" already exists. Please choose a different name.\n at Function.save (/home/laurent/source/joplin/ElectronClient/lib/models/Tag.js:184:55)"}

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [9 October 2020 10:17 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/18 "2020-10-09T10:17:48Z")

</div>

Now, I filed a bug for axios. . .

> <https://github.com/axios/axios/issues/3325>
>
> Describe the issue
> The code below uses node-fetch and axios to upload pictures, I don’t understand why axios fails
> Example Code
> Code snippet to...

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [9 October 2020 11:05 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/19 "2020-10-09T11:05:18Z")

</div>

Part of the issue, and I don't know if it's a problem with axios or Jest, is that when there's an error it just displays "failed with status code 500", but Joplin actually sends back detailed info about the error, which is not being displayed here. If it was it would be easy to tell if it's an error in the test, or Axios, or Joplin.

---

<div class="post-metadata">

### Author: ![rxliuli](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/rxliuli/32/4083_2.png) [@rxliuli](https://discourse.joplinapp.org/u/rxliuli)
#### Post date: [9 October 2020 11:07 UTC](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333/20 "2020-10-09T11:07:06Z")

</div>

But fetch succeeded, so. . .

[Next page](https://discourse.joplinapp.org/t/how-to-use-joplin-api-resources-id-file/11333.md?page=2)
