Problem using webwrite to send image through a simple REST API

조회 수: 22 (최근 30일)
Wei Chang
Wei Chang 2015년 9월 27일
댓글: seyed hamze 2023년 7월 4일
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
Abolfazl Nejatian 2021년 5월 6일
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??????????????????????????????????????

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by