Reputation: 622
I'm trying to post a file to PushBullet API service.
The info site of the API shows an example:
curl -i https://api.pushbullet.com/api/pushes \
-u API_KEY:
-F device_iden=u1qSJddxeKwOGuGW
-F type=file
-F [email protected]
And I'm trying to do it with HttpClient
from C#
I know how to post a file using Httpclient
, using MultipartFormDataContent
, but how can I add the device_iden and type info to the client?
I'm using this
using (var content = new MultipartFormDataContent())
{
try
{
content.Add(new StreamContent(new FileStream(pathFile, FileMode.Open, FileAccess.Read)));
var resp = wc.PostAsync(new Uri(baseUri, "api/pushes"), content);
}
catch (Exception)
{
throw;
}
}
[[New Added]]
Using cURL and fiddler this would be the POST
POST http://\/ HTTP/1.1
Authorization: Basic d45YQkxueWswSmxQRTYFjc1deUzNo8UtueVZpaktIZm34anVqdU9NZerWYmFlOp==
User-Agent: curl/7.33.0
Host: \
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 787
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------886f3981539a91b3
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="device_iden"
ujuOMfjVbaedjz7O3P0Jl6
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="type"
file
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="file"; filename="img.gif"
Content-Type: application/octet-stream
GIF89a������@�;A�;A�;A�;B�<B�<������!�Created with GIMP�!�
��,����������x�����I��8KȻ�`(�$��h��l{�p�tm߮��x����P,���҃l:q���I���u�Mf����
��d�y}LG��{���J�>���W};������^�1�����������
--------------------------886f3981539a91b3--
Upvotes: 2
Views: 2950
Reputation: 622
After trying with fiddler finally could make that post:
First:
public static void PushFile(string pathFile, string iddev)
{
string name = Path.GetFileName(pathFile);
using (var wc = GetClient())
{
using (var multiPartCont = new MultipartFormDataContent())
{
multiPartCont.Add(addStringContent("device_iden", iddev));
multiPartCont.Add(addStringContent("type","file"));
multiPartCont.Add(addStreamContent(new FileStream(pathFile,FileMode.Open),name ));
try
{
var resp = wc.PostAsync(new Uri(baseUri, "api/pushes"), multiPartCont);
Task<string> result = resp.Result.Content.ReadAsStringAsync();
//string resultado = result.Result;
}
catch (Exception)
{
throw;
}
}
}
}
Then methods to create the string content and stream content
private static StreamContent addStreamContent(Stream stream, string filename )
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
FileName = "\""+filename+"\""
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return fileContent;
}
private static StringContent addStringContent(string name, string content)
{
var fileContent = new StringContent(content);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"" + name + "\""
};
return fileContent;
}
Upvotes: 5
Reputation: 5414
Another way to create the -u
data is to create a HttpClientHandler
with NetworkCredential
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential(API_KEY, "")
};
var client = new HttpClient(handler);
This will create a basic http authentication header
Upvotes: 0
Reputation: 261
The -u in curl is the http authentication. It looks like pushbullet wants you to login with your API key. Using httpclient you would use;
httpClient.DefaultRequestHeaders.Authorization = CreateBasicHeader("username", "password");
The rest would be GET or POST variables (I'm not sure what pushbullet is expecting). It looks like in your code you have it setup with a POST. So you would append to your "content" variable with;
"device_iden=u1qSJddxeKwOGuGW&type=file";
Upvotes: 0