# The fetch api

**URL:** <https://forums.creativeclouddeveloper.com/t/the-fetch-api/688>\
**Category:** UXP Plugin API\
**Tags:** documentation\
**Created:** [March 5, 2019, 2:43pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688 "2019-03-05T14:43:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kilian](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kilian/32/299_2.png) [@Kilian](https://forums.creativeclouddeveloper.com/u/Kilian)\
**Post date:** [March 5, 2019, 2:43pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/1 "2019-03-05T14:43:53Z")

</div>

There are two things that caught me by surprise in the fetch api that aren’t mentioned in [the documentation](https://adobexdplatform.com/plugin-docs/reference/uxp/function/fetch.html)

- Only https is supported (this is unfortunate for any localhost usage)
- The request is cached and it’s unclear for how long and how to prevent this (other than the super hacky appending of `#${Math.random()}`)

---

<div class="post-metadata">

**Author:** ![stevekwak](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/stevekwak/32/48_2.png) [@stevekwak](https://forums.creativeclouddeveloper.com/u/stevekwak)\
**Post date:** [March 5, 2019, 4:26pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/2 "2019-03-05T16:26:51Z")

</div>

@Kilian A small suggestion: you could use [ngrok](https://ngrok.com/) to create a secure introspectable tunnel to localhost

---

<div class="post-metadata">

**Author:** ![ashryan](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ashryan/32/1177_2.png) [@ashryan](https://forums.creativeclouddeveloper.com/u/ashryan)\
**Post date:** [March 5, 2019, 7:23pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/3 "2019-03-05T19:23:50Z")

</div>

Actually, I think Kilian _did_ use ngrok in his demo at our XD plugin event in Amsterdam 🙂

---

<div class="post-metadata">

**Author:** ![Kilian](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kilian/32/299_2.png) [@Kilian](https://forums.creativeclouddeveloper.com/u/Kilian)\
**Post date:** [March 5, 2019, 7:39pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/4 "2019-03-05T19:39:40Z")

</div>

I did, it’s super useful! It’s not really something you could/should ship as part of an XD integration though.

---

<div class="post-metadata">

**Author:** ![cpryland](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/cpryland/32/1172_2.png) [@cpryland](https://forums.creativeclouddeveloper.com/u/cpryland)\
**Post date:** [March 5, 2019, 10:28pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/5 "2019-03-05T22:28:23Z")

</div>

The `fetch` documentation should also mention it won’t work with binary results like graphics.

---

<div class="post-metadata">

**Author:** ![peterflynn](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/peterflynn/32/56_2.png) [@peterflynn](https://forums.creativeclouddeveloper.com/u/peterflynn)\
**Post date:** [March 13, 2019, 10:35pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/6 "2019-03-13T22:35:41Z")

</div>

I _believe_ you can use `127.0.0.1` with regular http, even though `localhost` requires https. This is all based on the macOS App Transport Security feature, which by default blocks most insecure communications over the network. Why it applies that to localhost as well is a mystery to me, but I believe using the loopback IP address directly should get around it.

@kerrishotts, do you have any info on the other questions regarding caching and binary responses? I could swear we do support using `fetch()` to retrieve binary data…

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [March 18, 2019, 2:50pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/7 "2019-03-18T14:50:57Z")

</div>

I haven’t though to try using the loopback IP address… that would be a nice workaround.

As to SSL support, I know this will seem a distinction without a difference, but the API we built has no inherent restrictions on accessing insecure resources. Rather, it’s the host environment (macOS w/ ATS) that’s enforcing those restrictions. IIRC (and something to be careful of) Windows should access insecure resources without complaint.

For caching, it’s important to note that we use the OS’s networking APIs for XHR & Fetch. For example, on macOS, we use `NSURLSession`, which has a cache by default. It’s supposed to respect your response headers, so you should be able to configure your endpoint to expire the cache more frequently. (Let us know if this doesn’t hold, though.)

Finally, for binary data, we do support this, but not via `FormData` or `Blob`s yet. Instead, you have to use `ArrayBuffer`. Both `FormData` and `Blob` support is coming soon.

---

<div class="post-metadata">

**Author:** ![Velara](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/velara/32/1278_2.png) [@Velara](https://forums.creativeclouddeveloper.com/u/Velara)\
**Post date:** [September 3, 2019, 6:41pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/8 "2019-09-03T18:41:34Z")

</div>

I have some feedback as well. In some places the docs have helpful examples:

> [How to make network requests · Adobe XD Plugin Reference](https://adobexdplatform.com/plugin-docs/tutorials/how-to-make-network-requests/)

But for [fetch](https://adobexdplatform.com/plugin-docs/reference/uxp/function/fetch.html) I don’t see any examples and what I’ve tried I can’t get to work,

I’m attempting to get localhost (check that it’s running):

```
function testServerPath() {
	var serverPath = "http://localhost/";

	fetch(serverPath).then(function(response) {
		console.log("Response: ", response)
	});
}

```

If `http` is not supported (only https) shouldn’t it error out? Tested `https://www.google.com` and it responds but nothing from `http://www.google.com`.

**Update** :  
`http://127.0.0.1` does respond when a server is running

**Update 2:**  
Didn’t have catch on fetch but in the docs it says that it shouldn’t matter.

> The `fetch` specification differs from `jQuery.ajax()` in two main ways:
> 
> - The Promise returned from `fetch()` **won’t reject on HTTP error status** even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with `ok` status set to false), and it will only reject on network failure or if anything prevented the request from completing.

```
fetch(serverPath).then(function(response) {
	log("Response: ", response)
}).catch(function(error) { 
	log("Error occurred", error); // TypeError: Network request failed
})

```

So if someone was to check for localhost running on http what should they do? I can create another question.

Note: I’ve turned my local server on and off and I’m checking using fetch with a url of `http://127.0.0.1` and it’s still responding in both cases but visiting it in the browser it will report:

> **127.0.0.1** refused to connect.

when the server is turned off and load the home page when it is running.

---

<div class="post-metadata">

**Author:** ![Michel77](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Michel77](https://forums.creativeclouddeveloper.com/u/Michel77)\
**Post date:** [April 9, 2022, 11:03pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/9 "2022-04-09T23:03:25Z")

</div>

> [@kerrishotts](#):
>
> Both `FormData` and `Blob` support is coming soon.

Two years are over, still no blob support in PS?

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [April 10, 2022, 6:23pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/10 "2022-04-10T18:23:36Z")

</div>

The plumbing is in place – this works:

```nohighlight
const request = new Request('https://mdn.github.io/fetch-examples/fetch-request/flowers.jpg')
const response = await fetch(request);
const buffer = await response.arrayBuffer();
const imageBlobURL = URL.createObjectURL(new Blob([buffer]));
document.querySelector("img").setAttribute("src", imageBlobURL);

```

For some reason the `response.blob` method is _not_ present, even though `Blob` exists in the global namespace. @pkrishna / @Sujai can you take a look into why `fetch` isn’t registering a `blob` method on the response?

---

<div class="post-metadata">

**Author:** ![Nivedha](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/nivedha/32/1728_2.png) [@Nivedha](https://forums.creativeclouddeveloper.com/u/Nivedha)\
**Post date:** [May 9, 2022, 8:41am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/11 "2022-05-09T08:41:12Z")

</div>

@kerrishotts Is there a workaround for uploading an image to a URL which accepts multipart/form-data, since FormData is not supported ?

---

<div class="post-metadata">

**Author:** ![John677](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@John677](https://forums.creativeclouddeveloper.com/u/John677)\
**Post date:** [July 1, 2022, 7:47am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/12 "2022-07-01T07:47:39Z")

</div>

Is there planning blob support in PS? It’s missing.

Regards, John, manager, Work Time.

---

<div class="post-metadata">

**Author:** ![AndresLP](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AndresLP](https://forums.creativeclouddeveloper.com/u/AndresLP)\
**Post date:** [October 15, 2022, 10:00am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/13 "2022-10-15T10:00:15Z")

</div>

Hi @kerrishotts , the code works great in Phostoshop, thanks!. Now, would it be possible to save that file and then open it as a new Photoshop document? do I need to use batchPlay for that?

---

<div class="post-metadata">

**Author:** ![Symo470](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Symo470](https://forums.creativeclouddeveloper.com/u/Symo470)\
**Post date:** [October 18, 2022, 8:12pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/14 "2022-10-18T20:12:06Z")

</div>

Hiya @AndresLP,  
Do you mean you got this code working in your Photoshop Plugin? ie. opening a file within Photoshop from a URL source?

---

<div class="post-metadata">

**Author:** ![AndresLP](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AndresLP](https://forums.creativeclouddeveloper.com/u/AndresLP)\
**Post date:** [October 31, 2022, 7:23pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/15 "2022-10-31T19:23:41Z")

</div>

Hi @Symo470 , what I do is download the file and then open it in Photoshop:

```auto
  // Function to download file from url, save with user dialog, open in Photoshop
  async function downloadFileFromUrlAndOpen({ fileUrl, fileName }) {
    // Download file from url
    const request = new Request(fileUrl);
    const response = await fetch(request);
    const buffer = await response.arrayBuffer();

    // Save file to the filesystem
    const storage = require("uxp").storage;
    const file = await storage.localFileSystem.getFileForSaving(fileName);
    await file.write(buffer);
    try {
      await executeAsModal_openFile(file);
    } catch (e) {
      console.log(e);
    }
  }

```

```auto
/* Function to let user select a file, and open in Photoshop */
export function executeAsModal_openFile(file) {
  async function openFile(file) {
    console.log(file);
    if (file) {
      await require("photoshop").app.open(file);
    } else {
      return;
    }
  }

  try {
    require("photoshop").core.executeAsModal(async function () {
      openFile(file);
    });
  } catch (e) {
    if (e.number == 9) {
      showAlert(
        "executeAsModal was rejected (some other plugin is currently inside a modal scope)"
      );
    } else {
      console.log("exception!");
      // This case is hit if the targetFunction throws an exception
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Symo470](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Symo470](https://forums.creativeclouddeveloper.com/u/Symo470)\
**Post date:** [November 5, 2022, 3:19am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/16 "2022-11-05T03:19:53Z")

</div>

Thanks @AndresLP,  
What am I doing wrong if I get an:  
`Unexpected token 'export'`  
error upon launching your code?

---

<div class="post-metadata">

**Author:** ![AndresLP](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AndresLP](https://forums.creativeclouddeveloper.com/u/AndresLP)\
**Post date:** [November 5, 2022, 8:04am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/17 "2022-11-05T08:04:12Z")

</div>

Hi @Symo470 , you can take the “export” word out from the code, it shouldn’t be there unless you are calling the function from a separate file (as a JS module)

---

<div class="post-metadata">

**Author:** ![Symo470](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Symo470](https://forums.creativeclouddeveloper.com/u/Symo470)\
**Post date:** [November 6, 2022, 8:01am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/18 "2022-11-06T08:01:52Z")

</div>

Thanks @AndresLP. You can probably tell I’m pretty new at this…

So I’ve tried out your code and it doesn’t seem to do what I am expecting. Do I call the downloadFileFromUrlAndOpen function with strings for the fileUrl and fileName arguments?

I tried that and when I console.log the request variable is suggests that the url:“undefined”.

Appreciate your help.

---

<div class="post-metadata">

**Author:** ![AndresLP](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AndresLP](https://forums.creativeclouddeveloper.com/u/AndresLP)\
**Post date:** [November 7, 2022, 3:57pm UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/19 "2022-11-07T15:57:12Z")

</div>

Hi @Symo470 , are you sending fileUrl and fileName as object properties? something like this:

downloadFileFromUrlAndOpen(  
{  
fileUrl: “[https://www.someplace.com/file.jpg](https://www.someplace.com/file.jpg)”,  
fileName: “image name”  
}  
)

---

<div class="post-metadata">

**Author:** ![Symo470](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Symo470](https://forums.creativeclouddeveloper.com/u/Symo470)\
**Post date:** [November 9, 2022, 12:56am UTC](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688/20 "2022-11-09T00:56:39Z")

</div>

I’ve tried your suggestion, but I feel like the code hangs at the line:  
`const response = await fetch(request);`  
I’ve tried multiple Url’s for testing, but just no working for me.

[Next page](https://forums.creativeclouddeveloper.com/t/the-fetch-api/688.md?page=2)
