Problem using webwrite to send image through a simple REST API

16 次查看(过去 30 天)
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?

回答(1 个)

Vinod
Vinod 2016-10-14
Here's an example that uploads any file from MATLAB to DropBox programmatically:
Hopefully you can modify this example for your needs
  2 个评论
Abolfazl Nejatian
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 2023-7-4
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??????????????????????????????????????

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by