Problem using webwrite to send image through a simple REST API

11 views (last 30 days)
I was trying to do image classification through a web at service at https://cloudsight.readme.io/v1.0/docs and I had no problem to use curl to retrieve token:
curl -i -X POST -H "Authorization:MY Key" \
-F "image_request[image]=@/path/to/myimage" \
-F "image_request[locale]=en-US" \
https://api.cloudsightapi.com/image_requests
However, it was not successful when I tried webwrite, it returned "HTTP 400" error.
option=weboptions('KeyName','Authorization','KeyValue','mykey')
fid = fopen('/path/to/myimage');
img = fread(fid,Inf,'*uint8');
fclose(fid);
response=webwrite('https://api.cloudsightapi.com/image_requests',...
'image_request[image]',img,...
'image_request[locale]','en-US',option);
I guess it because the function webwrite in this format doesn't support "multipart/form-data" and I need change the media type. Then I tried send data as an JSON object
option=weboptions('KeyName','Authorization','KeyValue','mykey',...
'MediaType','application/json')
data=struct('image_request[image]',im,'image_request[locale]','en-US');
response=webwrite('https://api.cloudsightapi.com/image_requests',data,option)
But the field name in Matlab struct does not allow "[". Any suggestion?

Answers (1)

Vinod
Vinod on 14 Oct 2016
Here's an example that uploads any file from MATLAB to DropBox programmatically:
Hopefully you can modify this example for your needs
  2 Comments
Abolfazl Nejatian
Abolfazl Nejatian on 6 May 2021
Dear Vinod,
I have the same question.
I need to send some photos to the Telegram API.
I have tried what you said about the example, unfortunately, it doesn't help me.
here is the document of telegram for sending photos,
https://core.telegram.org/bots/api#sendphoto
and here is the python example of my goal
import requests
import json
bot_token = 'BOT TOKEN'
chat_id = "CHAT ID"
file = r"C:\Users\name\OneDrive\Desktop\Capture.PNG"
files = {
'photo': open(file, 'rb')
}
message = ('https://api.telegram.org/bot'+ bot_token + '/sendPhoto?chat_id='
+ chat_id)
send = requests.post(message, files = files)
if you could reform this python code to the matlab, I will appreciate your help.
thank in advance for your time
seyed hamze
seyed hamze on 4 Jul 2023
this is my question too. I search very much but they dont help me at all. I can send text to telegram but I cant send any photo from my computer to telegram bot.
I use tgprint() to send photo but it doesnt work.
Can anyone help me??????????????????????????????????????

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!