Hello. Please excuse my stupidity, I "sort off" know my way around B4x by now, but I am stuck with sending a document using UltraMSG api.
I got some sample code from there site for Java OKHttp
After some searching I kind off translated it to this
But when I run this code I get a response {"error":"Wrong token. Please provide token as a GET parameter."}
Is there anyone that can help me with this.
I can do it in VB.net with the code below
I got some sample code from there site for Java OKHttp
Sample Code:
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", "{TOKEN}")
.add("to", "")
.add("filename", "hello.pdf")
.add("document", "https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf")
.add("caption", "document caption")
.build();
Request request = new Request.Builder()
.url("https://api.ultramsg.com/{INSTANCE_ID}/messages/document")
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
After some searching I kind off translated it to this
My B4Code:
Dim WAToken As String = "mytoken"
Dim WAInstance As String = "myinstanceid"
Dim WAURL As String = "https://api.ultramsg.com/"& WAInstance & "/messages/document"
Dim j As HttpJob
j.Initialize("", Me)
Dim bodymap As Map
bodymap.Initialize
bodymap.Put("token", WAToken)
bodymap.Put("to", "278312345678")
bodymap.Put("filename", "hello.pdf")
bodymap.Put("document", "https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf")
bodymap.Put("caption", "New Order")
j.PostMultipart(WAURL, bodymap, Null)
j.GetRequest.SetHeader("token",WAToken)
' or whatever you need to set. Do it here
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
But when I run this code I get a response {"error":"Wrong token. Please provide token as a GET parameter."}
Is there anyone that can help me with this.
I can do it in VB.net with the code below
My VB.net Code:
Dim fi As FileInfo = New FileInfo(FileName)
Dim WebRequest As HttpWebRequest
Dim bytes As Byte() = IO.File.ReadAllBytes(FileName)
Dim file As String = Convert.ToBase64String(bytes)
WebRequest = HttpWebRequest.Create("https://api.ultramsg.com/" & WAInstanceID & "/messages/document")
Dim postdata As String = "token=" & WAToken & "&to=27" & MobileNumber & "&document=" & HttpUtility.UrlEncode(file) & "&filename=" & fi.Name
Dim enc As UTF8Encoding = New System.Text.UTF8Encoding()
Dim postdatabytes As Byte() = enc.GetBytes(postdata)
WebRequest.Method = "POST"
WebRequest.ContentType = "application/x-www-form-urlencoded"
WebRequest.GetRequestStream.Write(postdatabytes, 0, postdatabytes.Length)
' print return
Dim ret As New System.IO.StreamReader(WebRequest.GetResponse().GetResponseStream())
Dim RetStr As String
RetStr = ret.ReadToEnd()