Jump to content

multipart/form-data via fetchRemote


Recommended Posts

I have no idea how can I do this, had tried something but nothing happens.

I want to send the file via fetchRemote using multipart/form-data. Have someone experience in this stuff?

Link to post
  • Other Languages Moderators

👋. I can share an example of how to do this using the Imgur API.

local file = fileOpen ( "image.png", true )
local data = fileRead ( file, fileGetSize ( file ) )
data = base64Encode ( data )
fileClose ( file )

local sendOptions = {
    method = "POST",
    headers = {
        [ "Authorization" ] = "Client-ID <YOUR_CLIENT_ID>", -- replace <YOUR_CLIENT_ID> with your client ID
        [ "Content-Type" ] = "multipart/form-data"
    },
    formFields = {
        [ "image" ] = data,
        [ "type" ] = "base64"
    }
}

fetchRemote ( "https://api.imgur.com/3/upload", sendOptions,
    function ( data, info )
        iprint ( data, info )
    end
)

The image file (image.png) should be located in the resource folder.

  • Like 1
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...