How to upload a file in Telegram Bott api using Http client? - upload

I have tried to send files using multipart form data, but all I get is the following error:
{"description":"Error: Bad Request: chat_id is empty","error_code":400,"ok":false}
Here is my code snippet. Can anyone help me out where I am committing an error?
public HttpResponse<jsonnode> sendDocument(Integer chat_id,File f1) throws UnirestException {
return Unirest.post(endpoint + token + "/sendDocument")
header("accept", "application/json")
.field("chat_id", chat_id)
.field("document", f1)
.asJson();
}

well, it said chat_id is empty. that's an obvious error!
but for your question, there is only two ways to sendDocument to telegram.
that file is already is in their servers, so you should just pass the file_id in "document" field
you want to upload a file from your device and as they said
Must be posted using multipart/form-data in the usual way that files are uploaded via the browser
if you are doing the upload section correct, then just make sure chat_id is not empty.

Related

Unable to upload large file in MS Graph Onedrive

I am using PUT method to upload in Onedrive
url= https://graph.microsoft.com/v1.0//users/{user-id}/drive/items/{item-path}:/{filename}:/content
var response = MSGraphHelper.InvokeGraphPUTStream(url, streamcontent)
The above is working properly but only for below 4MB file
I added a condition to support large file upload.
if (response == "RequestEntityisTooLarge")
{
sessionUrl = https://graph.microsoft.com/v1.0//users/{userId}/drive/items/{item-path}/createUploadSession
var uploadUrl = MSGraphHelper.InvokeGraphPost(url, null)
I got the uploadUrl from the POST method above but when I do the PUT method using the uploadUrl, I got "Bad Request" error.
I don't know what went wrong.Please help
Can you verify that your request is in the format similar to below example? I was able to get this working in Postman to create an upload session. The request body is optional.
POST https://graph.microsoft.com/v1.0/sites/root/drive/root:/PostmanCreatedFile1:/createUploadSession
Additional syntax for HTTP requests can be found here:
https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#http-request

Microsoft graph: Resumable upload doesnt work if the file name contains Norwegian character(ø)

I am using resumable file upload which works fine for files without Norwegian character in it. If the file name has Norwegian character in it the uploadSession is established and after that when I sent the content on the uploadUrl returned from upload session request the call fails and gives the following error.
{
"error":"invalid_request",
"error_description":"This access token is not valid on this endpoint."
}
However from the same file if I remove this character (ø) everything works fine.
This the sample url returned from the createUploadSession endpoint.
https://<sharepoint_sitename>/sites/jnu/_api/v2.0/drives/b!uey_5Lpf8Eu0-5HlGvJvFga89s_hTxNAg9kSsTlOBqajRqOF3ec-Rp6uqtbLNt4w/items/016GMDDVAF3UIOU2YNWVE3TX24ZV44WN4K/uploadSession?guid=%274ce82b5c-ede4-4d1d-af0a-6ff3b33c10b7%27&path=%27%7etmp41_Decisions+-+Advania+sdfsldfasduftest%u00f8+a%27&overwrite=True&rename=False&tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvZmlnZy5zaGFyZXBvaW50LmNvbUBjNzEwNWRkYS1hNTQ4LTRkZWYtOGVlNy1lOTkyZTBlNmRjYjAiLCJpc3MiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAiLCJuYmYiOiIxNTAyMDk4NDM0IiwiZXhwIjoiMTUwMjE4NDgzNCIsImVuZHBvaW50dXJsIjoiQ1A3T2Q0OVBKR25Scy9TbnN0akNUbk5BV29KeEJhdzFVNnNCTHY5SG5oND0iLCJlbmRwb2ludHVybExlbmd0aCI6IjMwMiIsImlzbG9vcGJhY2siOiJUcnVlIiwiY2lkIjoiWW1JMk1tUTFaamd0TkRZMllpMDBaR1JtTFdJM1lqY3ROVE5qWTJNMU1UZ3lNbU5qIiwidmVyIjoiaGFzaGVkcHJvb2Z0b2tlbiIsInNpdGVpZCI6IlpUUmlabVZqWWprdE5XWmlZUzAwWW1Zd0xXSTBabUl0T1RGbE5URmhaakkyWmpFMiIsIm5hbWVpZCI6IjAjLmZ8bWVtYmVyc2hpcHxkZWVwYWsuc2hhcm1hQGRlY2lzaW9ucy5ubyIsIm5paSI6Im1pY3Jvc29mdC5zaGFyZXBvaW50IiwiaXN1c2VyIjoidHJ1ZSIsImNhY2hla2V5IjoiMGguZnxtZW1iZXJzaGlwfDEwMDM3ZmZlOTY2MjcxYmNAbGl2ZS5jb20iLCJ0dCI6IjAiLCJ1c2VQZXJzaXN0ZW50Q29va2llIjpudWxsfQ.MTl0bEZ6cldwUlZEVUkwcEphUkpnWDRVelNnWnFWQndlVEtpbG9ndlhLYz0
This was a regression introduced when the session URLs were updated to a new format - it should now be fixed!

AWS Pre-signed PUT URL Requires &s to be Replaced with &?

I have a service that generates S3 pre-signed PUT URLS for uploading.
While testing I noticed that Postman would have no issue uploading to the pre-signed URL as long as I clicked on the URL in "Pretty" mode. This would put the URL in its address bar.
But when I copied the raw output, it would 403 every time.
Does AWS S3 require & signs to be replaced with &?
EDIT: Updating to show how pre-signed URL is fetched with Node/Express/AWS API
router.get('/', function (req, res) {
var s3 = new AWS.S3();
var params = {Bucket: 'some-s3-bucket', Key: 'test/'};
s3.getSignedUrl('putObject', params, function (err, url) {
res.json({url: url});
});
});
No, you shouldn't html-encode & → & in the query string separators in an S3 signed URL.
It sounds like your service is generating the urls and either it, or something in the chain, is incorrectly html-encoding them, and by luck, postman or the browser is cleaning them back up again when you view them in "pretty" mode.
If you're deliberately doing this, then don't... but if you're unclear where the spurious encoding is coming from, switching to a text based tool for talking to your service -- like curl or wget -- is likely to get you closer to understanding what's going on than a browser-based tool.

Google docs API: can't download a file, downloading documents works

I'm trying out http requests to download a pdf file from google docs using google document list API and OAuth 1.0. I'm not using any external api for oauth or google docs.
Following the documentation, I obtained download URL for the pdf which works fine when placed in a browser.
According to documentation I should send a request that looks like this:
GET https://doc-04-20-docs.googleusercontent.com/docs/secure/m7an0emtau/WJm12345/YzI2Y2ExYWVm?h=16655626&e=download&gd=true
However, the download URL has something funny going on with the paremeters, it looks like this:
https://doc-00-00-docs.googleusercontent.com/docs/securesc/5ud8e...tMzQ?h=15287211447292764666&amp\;e=download&amp\;gd=true
(in the url '&amp\;' is actually without '\' but I put it here in the post to avoid escaping it as '&').
So what is the case here; do I have 3 parameters h,e,gd or do I have one parameter h with value 15287211447292764666&ae=download&gd=true, or maybe I have the following 3 param-value pairs: h = 15287211447292764666, amp;e = download, amp;gd = true (which I think is the case and it seems like a bug)?
In order to form a proper http request I need to know exectly what are the parameters names and values, however the download URL I have is confusing. Moreover, if the params names are h,amp;e and amp;gd, is the request containing those params valid for obtaining file content (if not it seems like a bug).
I didn't have problems downloading and uploading documents (msword docs) and my scope for downloading a file is correct.
I experimented with different requests a lot. When I treat the 3 parameters (h,e,gd) separetaly I get Unauthorized 401. If I assume that I have only one parameter - h with value 15287211447292764666&ae=download&gd=true I get 500 Internal Server Error (google api states: 'An unexpected error has occurred in the API.','If the problem persists, please post in the forum.').
If I don't put any paremeters at all or I put 3 parameters -h,amp;e,amp;gd, I get 302 Found. I tried following the redirections sending more requests but I still couldn't get the actual pdf content. I also experimented in OAuth Playground and it seems it's not working as it's supposed to neither. Sending get request in OAuth with the download URL responds with 302 Found instead of responding with the PDF content.
What is going on here? How can I obtain the pdf content in a response? Please help.
I have experimented same issue with oAuth2 (error 401).
Solved by inserting the oAuth2 token in request header and not in URL.
I have replaced &access_token=<token> in the URL by setRequestHeader("Authorization", "Bearer <token>" )

Gzip decompress JSON POST body in Rails/Passenger/Nginx

We have a function in our Rails code that accepts a JSON POST body:
contacts = ActiveSupport::JSON.decode(request.raw_post.gsub("+", ""))
(I'm aware that I can get this from params["_json"] as well, but we have extremely large (MBs) POST bodies that do not get put into params["_json"] for some reason (and + throws errors too).
Since the JSON is usually sent from a mobile client, it's important to us to optimize the upload size. We want to switch to having the POST body gzipped.
However, no matter what we do, we get the same error with no line number:
MultiJson::DecodeError (743: unexpected token at ''):
We have tried:
gzipped_contacts = Zlib::GzipReader.new(StringIO.new(request.raw_post)).read
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
This:
gzipped_contacts = ActiveSupport::Gzip.decompress(request.raw_post)
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
And the solution found here: Rails: how to unzip a compressed xml request body?
I'm pretty sure this is not occurring at the controller level because I can't log anything there, so it needs to be done in the middleware or at the server (but I can't find anything for Nginx that lets us deflate). Please assist!
Ok, turns out the iPhone client was sending the wrong headers. So the solution for anyone encountering this is to see the advice here:
Rails: how to unzip a compressed xml request body?
And verify that you are sending Content-Type: gzip/json.

Resources