Skip to main content
Question

Error making a POST request in a create-figma-plugin codebase

  • December 6, 2022
  • 3 replies
  • 1253 views

neicore

Hello,
I am working on this Figma plugin and I am using create-figma-plugin toolkit. I want to integrate the plugin with LemonSqueezy’s license key API. I am making a post request and it doesn’t work giving this error:

Error: in fetch: Expected "init.body" to have type Uint8Array but got string instead
    at Proxy.fetch (eval at <anonymous> (eval at createScopedEvaluatorFactory (671d607b-e167-4ac8-a492-12b74d724c97:1:6906)), <anonymous>:23:21)
    at doit (PLUGIN_1_SOURCE:114:29)
    at eval (PLUGIN_1_SOURCE:151:7)
    at eval (PLUGIN_1_SOURCE:56:7)
    at invokeEventHandler (PLUGIN_1_SOURCE:62:35)
    at figma.ui.onmessage (PLUGIN_1_SOURCE:73:11)
    at ptr.callFunction (figma_app.min.js.br:717:4755)
    at iframeMessageHandler (figma_app.min.js.br:1440:17341)
    at EIo.toplevelWindowMessageChannel.port2.onmessage (figma_app.min.js.br:1409:5506)

Here is the code:

try {
      const res = await fetch(
        'https://api.lemonsqueezy.com/v1/licenses/activate',
        {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: 'license_key=38b1460a-5104-4067-a91d-77b872934d51&instance_name=Test',
        }
      )

      const data = await res.json()
      console.log(data)
    } catch (error) {
      console.log(error)
    }

The same code works in codesandbox and in a code base using vanilla js.

Any help is appreciated, thanks

3 replies

Palash
  • 1 reply
  • December 20, 2022

You might need to convert your “body” to a Unit8Array using TextEncoder

See this stackoverflow answer for an example: javascript - Converting between strings and ArrayBuffers - Stack Overflow

Also, I hope that’s not your actual API key, if it is, you might want to regenerate it😅


neicore
  • Author
  • 4 replies
  • December 22, 2022

Thanks for the reply, been there it didn’t work. I have since abandoned the feature 😦 And not that’s not my key, it’s from the doc 🙂


Gleb
  • Power Member
  • 4706 replies
  • December 22, 2022

Not sure about create-figma-plugin specifically, but in general I would recommend you do the fetch request inside of the plugin UI instead of plugin backend. The built-in fetch in the plugin backend is not a full-featured replication of the actual fetch. It still has some sandboxed limitations.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings