Following is my code (I need form_data to upload a file, but for now, test with a source: "https://image_url"
is enough.
post "/api/upload" do
# file_path = params.from_multipart.last["source"].path
io = IO::Memory.new
form_data = HTTP::FormData::Builder.new(io)
form_data.field("key", "my_key")
form_data.field("source", "https://avatars.githubusercontent.com/u/549126?s=96&v=4")
response = HTTP::Client.post(
url: "https://freeimage.host/api/1/upload",
headers: HTTP::Headers{"Content-Type" => "multipart/form-data"},
form: io
)
pp! response
if response.success?
image_url = JSON.parse(response.body).dig("image", "display_url")
json({image_url: image_url}, HTTP::Status::OK)
else
json(HTTP::Status::BAD_REQUEST)
end
end
When I upload, I always get error like this:
{\"status_code\":400,\"error\":{\"message\":\"Invalid API v1 key.\",\"code\":100},\"status_txt\":\"Bad Request\"}"
But if i test use a tool like postman, it works (check following screenshot)
Following is the screenshot of API page
I have another question.
Either a image URL or a base64 encoded image string. You can also use FILES[“source”] in your request.
What is the means of FILES["source"] in your request
, how to do that use HTTP::FormData::Builder
?
thanks