Uploading files with HTTP PUT

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1
How do i use HTTP PUT to upload files?

Im using C# code to send the request

Код:
var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"link",
"",
"application/x-www-form-urlencoded",
instance.GetProxy(),
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderAndBody,
AdditionalHeaders: new []{"Authorization:Bearer " + oauth" }
);

return response;
how do i upload files with this?
or is there a better alternative?

Thanks in advance
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 455
Благодарностей
5 913
Баллы
113
It's the same as POST with multipart:
var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
@"-----------------------------77748066757751
Content-Disposition: form-data; name=""file""; filename=""cat.jpg""
Content-Type: image/jpeg

C:\Downloads\multipart\cat.jpg
-----------------------------77748066757751--",
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
 
  • Спасибо
Реакции: tsuna123

ffeniks

Client
Регистрация
03.06.2016
Сообщения
312
Благодарностей
410
Баллы
63
It's the same as POST with multipart:
Код:
var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
project.Variables["url_upload"].Value,
@"-----------------------------77748066757751
Content-Disposition: form-data; name=""file""; filename=""123.rar""
Content-Type: application/x-rar-compressed

C:\папка\папка\123.rar
-----------------------------77748066757751--",
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
Использую данный код, в яндекс диск rest api, rar создаётся, но не закачивается.
При скачивании битый архив. В чём может быть проблема? С txt та же беда, создаётся новый файл с текстом директории (C:\папка\папка\123.txt).
Хотя в файле 123.txt - лежит другой текст

Код:
PUT /a/otpusk.avi HTTP/1.1
Host: webdav.yandex.ru
Accept: */*
Authorization: OAuth 0c4181a7c2cf4521964a72ff57a34a07
Etag: 1bc29b36f623ba82aaf6724fd3b16718
Sha256: T8A8H6B407D7809569CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592
Expect: 100-continue
Content-Type: application/binary
Content-Length: 103134024

<содержимое файла>
 
Последнее редактирование:

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1
It's the same as POST with multipart:
Thanks for the help but i dont think its working for me...

When i check the request traffic, this is what the request body looks like when i do it on the website

This is what the request body looks like when i try it with the code

Doesnt look like the file is being chosen properly?

Also,

@"-----------------------------77748066757751
-----------------------------77748066757751--",

What do these parts mean?
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 455
Благодарностей
5 913
Баллы
113
Thanks for the help but i dont think its working for me...

When i check the request traffic, this is what the request body looks like when i do it on the website

This is what the request body looks like when i try it with the code

Doesnt look like the file is being chosen properly?

Also,

@"-----------------------------77748066757751
-----------------------------77748066757751--",

What do these parts mean?
How do you encode a file in your request? In bytes?
 

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1
How do you encode a file in your request? In bytes?
can you show me how i can take a image link convert to Bytes and upload it using the PUT request like the one u showed above ?

string Url = "https://image_link";
using (var webClient = new System.Net.WebClient()) {
byte[] imageBytes = webClient.DownloadData(Url);

}

var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
imageBytes[0],
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
im trying this but its not working, i already imported all references/dll needed for webclient, only issue is HTTP PUT
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 455
Благодарностей
5 913
Баллы
113
H
can you show me how i can take a image link convert to Bytes and upload it using the PUT request like the one u showed above ?



im trying this but its not working, i already imported all references/dll needed for webclient, only issue is HTTP PUT
Here are examples on how to convert image to bytes - https://stackoverflow.com/questions/3801275/how-to-convert-image-to-byte-array
You may convert image like this instead of webclient:
Код:
byte[] mageBytes  = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(path));
 

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1
@VladZen please help me with this last part ive been trying for a while but no luck
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 455
Благодарностей
5 913
Баллы
113
hi thanks, i already have converted the image into bytes but im not sure how to add it into the PUT request
I don't quite understand what problem you have...
This code converts the image to bytes automatically. all you need is just insert link to image at the beginning.
And put the link to website where to upload image in var response string.
Код:
string Url = "https://image_link";
using (var webClient = new System.Net.WebClient()) {
byte[] imageBytes = webClient.DownloadData(Url);

}

var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
imageBytes[0],
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
 

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1
I don't quite understand what problem you have...
This code converts the image to bytes automatically. all you need is just insert link to image at the beginning.
And put the link to website where to upload image in var response string.
Код:
string Url = "https://image_link";
using (var webClient = new System.Net.WebClient()) {
byte[] imageBytes = webClient.DownloadData(Url);

}

var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
imageBytes[0],
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;

i tried running that code but is is not runable,
this is what is says in the log "
Compile code of Error in action "CS0103" "The name 'imageBytes' does not exist in the current context". [Row: 9; Column: 1]
"

Код:
byte[] imageBytes;
string Url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
using (var webClient = new System.Net.WebClient()) {
imageBytes = webClient.DownloadData(Url);
}

var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
imageBytes[0],
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
So i declared imageBytes outside but i am still geting error message:

Compile code of Error in action "CS1502" "The best overloaded method match for 'ZennoLab.CommandCenter.ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod, string, string, string, string, string, ZennoLab.InterfacesLibrary.Enums.Http.ResponceType, int, string, string, bool, int, string[], string, bool, bool, ZennoLab.InterfacesLibrary.ProjectModel.ICookieContainer)' has some invalid arguments". [Row: 7; Column: 16]

Compile code of Error in action "CS1503" "Argument 3: cannot convert from 'byte' to 'string'". [Row: 9; Column: 1]
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 455
Благодарностей
5 913
Баллы
113
i tried running that code but is is not runable,
this is what is says in the log "
Compile code of Error in action "CS0103" "The name 'imageBytes' does not exist in the current context". [Row: 9; Column: 1]
"

Код:
byte[] imageBytes;
string Url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
using (var webClient = new System.Net.WebClient()) {
imageBytes = webClient.DownloadData(Url);
}

var response = ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.PUT,
"https://httpbin.org/put",
imageBytes[0],
"multipart/form-data",
"",
"",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly
);
return response;
So i declared imageBytes outside but i am still geting error message:

Compile code of Error in action "CS1502" "The best overloaded method match for 'ZennoLab.CommandCenter.ZennoPoster.HTTP.Request(ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod, string, string, string, string, string, ZennoLab.InterfacesLibrary.Enums.Http.ResponceType, int, string, string, bool, int, string[], string, bool, bool, ZennoLab.InterfacesLibrary.ProjectModel.ICookieContainer)' has some invalid arguments". [Row: 7; Column: 16]

Compile code of Error in action "CS1503" "Argument 3: cannot convert from 'byte' to 'string'". [Row: 9; Column: 1]
Something wrong with arguments in this method... - https://help.zennolab.com/en/v5/zennoposter/5.27.0.0/topic758.html
 

tsuna123

Новичок
Регистрация
29.10.2018
Сообщения
11
Благодарностей
0
Баллы
1

Кто просматривает тему: (Всего: 5, Пользователи: 1, Гости: 4)