[SOLVED] How to upload an image using POST API

What did you need to do to encode the .jpg to base64?

Same guy, I just donā€™t feel like switching to my primary account.

So I used Livecode, which has a command to encode the data as base 64.

After fiddling a long time with the ā€œdetect dataā€-function to get this to work I tried it with just adding the data format myself.
Just put the Key ā€œfileā€ and Type ā€œimageā€ by hand and it magically works.

Wondering if anyone has had luck with this since. I believe Iā€™ve got my JSON object formatted properly, being passed to my endpoint, but the result I get in the bubble db is a blank image.

cc @iggyvanlith @emmanuel

Look correctly at the first part of your video (Emmanual Post).
The file information are store in a ā€œfileā€ parameter (you can call it whatever you want)
this give

{
	"file": {
		"filename": "filename.jpg",
		"contents": "base64 encoded data"
	}
}
1 Like

Thanks @Jici, Iā€™ve tried with that exact format as well (with the file JSON object, then filename and contents stored in that object) but no luck. Just retested and Iā€™m still getting this as the record in the DB.

What did you set for the initialize setting of ā€œfileā€?

1 Like

@Jici thanks for the help here ā€“ hereā€™s the initialize call (1st image) and the api workflow detect data result for the endpoint Iā€™m trying to set up


should the ā€œcontentsā€ data be set as a file as Iā€™m doing?

1 Like

I forget. But if you use autodetect, the file is split in two part. You can save contents to field (just set image or field) but you need to save the name in another field.
I test it with
data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5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hAAOw==
without any problem.

Now if you want to save it to s3, I recommend to manually encode the api data. It should save to s3 this way with the filename.

@Jici or others - how does one save the base64 as a file? My original assumption was that the file contents were going to be saved to S3 and referencing that as the source of the the image I create.

When creating the Image record (like first image) Iā€™m still getting it as a Base64 filetype (second image)


Dear blueback09, could you please advice how to do this : 10. setup your API to recognize the ā€œfileā€ parameter as a file and the ā€œexampleā€ parameter as text

Thanks in advance

@kirill_anikin my impression of that was

  1. In Api Workflows, you can either use ā€˜Detect Request Dataā€™ or ā€˜Manual definitionā€™
    https://cl.ly/9794b3fa5ea1
  2. Initialize the endpoint, which will show you the parameters youā€™re passing into it (ie via a POST endpoint)
    https://cl.ly/c5d5c71988da
  3. Define how you want to use the data youā€™re passing to the endpoint (ie as File if desired)
    https://cl.ly/357019d358ac

Still stuck here and have solicited a few great Bubble minds already asking for their take on this. No luck :confused:

Link to editor: Proof-of-concept | Bubble Editor

API workflow in question is the ā€œS3 saveā€ workflow, which Iā€™m hoping to pass a file (base64 image) and a name (text) via a Post endpoint Iā€™ve set up. Iā€™d then like to have Bubble create a new image record, saving it with a URL that I can link to, as it would for images Iā€™d upload using image uploader element. Use case for storing Base64 image as an image file w/ URL is for a text editor Iā€™m using making in a CMS. Users will upload an image to the text editor interface Iā€™m using and Iā€™d prefer to not have the long Base64 image data in the HTML markup.

Whenever I save, the data in the Image field is stored as Base64 (so it teases me, rendering as an image) but if you look at the image source itā€™s Base64 like second link below.


When it works, I also expect this song to play

1 Like

Hi @eric3, I tweaked your project, and it works now. Check it to see the details. The tma1.jpg image is what I posted through your endpoint.

I think the issue was that the ā€œInitialize / Detect request dataā€ feature should NOT be used. Instead, manually define the parameters as shown. The file parameter can be called whatever you want (ā€œuploaded_imageā€ in this case), as @Jici noted. Additional ā€œfield valuesā€ for the ā€œthingā€ being created can be defined at the JSON root.

Hereā€™s a snippet of the payload sentā€¦

{
  "title": "Sorry, Dave",
  "uploaded_image": {
    "filename": "tma1.jpg",
    "contents": "/9j/4AAQSkZJRg [...]"
  }
}

EDIT: As noted in the reference, private and attached_to properties can also be added to uploaded_image.

5 Likes

Amazing, thanks for that pointer! Confirmed itā€™s working now, will post a video with working version for others :slight_smile:

2 Likes

:slight_smile:

Of course I was looking through to wrong end of the telescope as usualā€¦

Not having the image in base64 in the first place is a lot easier than trying to convert it to an image file in Bubble ! (which will probably need a server side plugin and a little bit of code).

One more quick note (which @NigelGā€™s reference to base64 encoding prompted)ā€¦

The base64-encoded image should NOT contain the ā€œdata:mimetype;base64ā€ prefix. Thatā€™s needed only in the context of a data URL - i.e. only if you intend to store the image data itself directly in the DB so that the image data is part of the URL and thus served with the page source and rendered ā€œinlineā€.

That could make sense in some situations for relatively small images, but itā€™s generally not the best way to go for most images.

3 Likes

Dear eric3. thanks for your help. Could you please comment how do your fill in point 2. filed Body {JSON object,ā€¦ā€¦ā€¦ā€¦ it looks like you put data directly in bubble(base64 coded image)ā€¦.but what to wright there if we want to send to endpoint different files?

Thanks a lot in advance

You can use the same pattern with different name for each ā€œfileā€

{
  "title": "Sorry, Dave",
  "uploaded_image": {
    "filename": "tma1.jpg",
    "contents": "/9j/4AAQSkZJRg [...]"
  },
  "uploaded_image2": {
    "filename": "tma2.jpg",
    "contents": "/9j/4AAQSkZJRg [...]"
  }
}
1 Like

Thanks Jici, but may be it should be like this:
{
ā€œtitleā€: <ā€œSorry, Daveā€>,
ā€œuploaded_imageā€: {
ā€œfilenameā€: <ā€œtma1.jpgā€>,
ā€œcontentsā€: <"/9j/4AAQSkZJRg [ā€¦]">
},
ā€œuploaded_image2ā€: {
ā€œfilenameā€: <ā€œtma2.jpgā€>,
ā€œcontentsā€: <"/9j/4AAQSkZJRg [ā€¦]">
}
} I what file name to be variable