Reputation: 195
i'm trying to get data from local file and add data into one of the file into adls(azure data lake store) i'm following the below URL. click here
how can i pass parameters and body to the api using c# because so many headers don't have any value
below code i tried to execute but no success.
HttpClient httpClient = new HttpClient();
{
String storageAccountUrl = "https://XXXXXXXXXX.dfs.core.windows.net/XXXXX/test.txt?action=flush&position=0&retainUncommittedData=false&close=true";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
var httpResponseMessage = await httpClient.PutAsync(storageAccountUrl, new StringContent("test"));
string resp = await httpResponseMessage.Content.ReadAsStringAsync();
}
how should i pass other headers and is it the right way to pass body.
Upvotes: 1
Views: 649
Reputation: 30025
The code below is working fine if update a empty file:
static void Main(string[] args)
{
var auth = new AzureServiceTokenProvider();
const string url = "https://storage.azure.com/";
string token = auth.GetAccessTokenAsync(url).Result;
string requestUri = "https://xxx.dfs.core.windows.net/t11/c.txt?action=append&position=0";
var method = new HttpMethod("PATCH");
string upload_string = "have a nice day!";
var request = new HttpRequestMessage(method, requestUri)
{
Content = new StringContent(upload_string)
};
// Add some defined headers
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
var i = request.Content.AsString().Length;
Console.WriteLine(request.Content.AsString());
var httpClient = new HttpClient();
var result = httpClient.SendAsync(request).Result;
Console.WriteLine("append result status code: "+ (int)result.StatusCode);
//for flush
string requestUri_2 = "https://xxx.dfs.core.windows.net/t11/c.txt?action=flush&position="+upload_string.Length;
var request_2 = new HttpRequestMessage(method,requestUri_2);
using (HttpClient httpClient_2 = new HttpClient())
{
httpClient_2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = httpClient_2.SendAsync(request_2).Result;
Console.WriteLine("flush result status code: " + (int)response.StatusCode);
}
Console.ReadLine();
}
Upvotes: 1
Reputation: 5549
Based on the documentation, some headers are just optional which means that you do not need to set them in the headers. You only need to set the required headers.
Update:
For how to use Httpclient to send a patch request in C#, you may get a lot of samples and documentations on the internet. Here is just a sample:
static void Main(string[] args)
{
string requestUri = "the uri, https://.....";
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = new StringContent("Test string")
};
// Add some defined headers
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Add some other headers or custom headers
request.Headers.TryAddWithoutValidation("custome_header", "value");
var hc = new HttpClient();
var result = hc.SendAsync(request).Result;
Console.WriteLine(result);
Console.ReadLine();
}
Upvotes: 1