B4J Question How to send multipart request correctly

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hello all ,


I am trying to upload a picture to Telegram using Bot api but with no luck
Here is my code:

B4X:
    Dim fd As FileData
    fd.Initialize
    fd.Dir = path
    fd.FileName = fname
    fd.KeyName = "photo"
    fd.ContentType = "multipart/form-data"
    Dim files As List
    files.Initialize
    files.Add(fd)
    hc.Initialize("hc")
    Dim req As HttpRequest
    Dim NV As Map
    NV.Initialize
    NV.Put("chat_id", chat_id)
    NV.Put("caption", caption)
    req=MultipartPost.CreatePostRequest(link&"/sendPhoto",NV,files)
    hc.Execute(req,  1)

and here is method documentation

https://core.telegram.org/bots/api#sendphoto

I get 415 status code means Unsupported Media Type .

Can any one please help me and tell what to change in the code to make it work .

By the way sending message is working correctly (using GET) which means there is no problems in api connection .
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
You can download jOkHttpUtils2: https://www.b4x.com/android/forum/threads/okhttputils2.62105/

It supports multipart requests. My guess is that the content type is incorrect. It should probably be image/* or something like that.

I got this error :

B4X:
Program started.
java.io.EOFException
    at okio.RealBufferedSource.require(RealBufferedSource.java:64)
    at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.java:270)
    at com.squareup.okhttp.internal.http.HttpConnection$ChunkedSource.readChunkSize(HttpConnection.java:479)
    at com.squareup.okhttp.internal.http.HttpConnection$ChunkedSource.read(HttpConnection.java:460)
    at okio.Buffer.writeAll(Buffer.java:956)
    at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
    at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:57)
    at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:83)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:211)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
photo Unsupported Media Type
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Can you post your code?

B4X:
Sub sendPhoto(chat_id As String,path As String,fname As String,caption As String)
    Dim j As HttpJob
    j.Initialize("photo", Me)
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "photo"
    fd.Dir = path
    fd.FileName = fname
   

    fd.ContentType = "image/jpeg"
    j.PostMultipart(link&"/sendPhoto", CreateMap("chat_id":chat_id , "caption": caption) , Array(fd) )
   
   
   
End Sub

Try
B4X:
fd.ContentType = "image/jpeg"
when uploading a jpeg

Still the same .


And I get the same "Unsupported Media Type " even when sending the post without a file .
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Search for an example in a another language (or curl) and we can help you translate it.

Here is a PHP script


B4X:
$bot_url    = "https://api.telegram.org/bot<bot_id>/";
$url        = $bot_url . "sendPhoto?chat_id=" . $chat_id ;

$post_fields = array('chat_id'   => $chat_id,
    'photo'     => new CURLFile(realpath("/path/to/image.png"))
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);

Got it from here

http://stackoverflow.com/questions/32296272/telegram-bot-api-how-to-send-a-photo-using-php
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tried it with this code and I don't get any error:
B4X:
Sub sendPhoto(chat_id As String,path As String,fname As String,caption As String)
  Dim j As HttpJob
  j.Initialize("photo", Me)
  Dim fd As MultipartFileData
  fd.Initialize
  fd.KeyName = "photo"
  fd.Dir = "c:\temp\"
  fd.FileName = "1.png"
   

  fd.ContentType = "image/jpeg"
  j.PostMultipart("https://core.telegram.org/bots/api#sendphoto", CreateMap("chat_id":chat_id , "caption": caption) , Array(fd) )
End Sub
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User

You changed url How can server identify my bot ?
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
try it with a url like
PHP:
$bot_url = "https://api.telegram.org/botMY_BOT_TOKEN/";
where MY_BOT_TOKEN should be replaced with your bot token...
That is what I am trying .

Also this sub is working (GET request )

B4X:
Public Sub sendMessage(chat_id As String,text As String)
   
    Dim send As HttpJob
    send.Initialize("send",Me)
    send.Download2(link&"/sendMessage",Array As String("text",text,"chat_id",chat_id))
   
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…