How to add content title to Flurl

I would like to know how to add content header to flurl-statement. The onedrive implementation requires me to add a content type header to the content and try every possible solution every time.

I am forced to use plain httpclient with the following code.

Public Async Function UploadFile(folder As String, filepath As String) As Task(Of Boolean) Implements ICloud.UploadFile
        Dim data As Byte() = File.ReadAllBytes(filepath)
        Dim uploadurl As String = "drive/items/" + folder + ":/" + Path.GetFileName(filepath) + ":/" + "content?access_token=" + Token.access_token


        Using client As New HttpClient()
            client.BaseAddress = New Uri(ApiUrl)

            Dim request As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Put, uploadurl)

            request.Content = New ByteArrayContent(data)
            request.Content.Headers.Add("Content-Type", "application/octet-stream")
            request.Content.Headers.Add("Content-Length", data.Length)

            Dim response = Await client.SendAsync(request)

            Return response.IsSuccessStatusCode
        End Using
    End Function

I already tried the usual PutJsonAsync method for Flurl, but no luck. This is the only non-flurl part remaining in my code.

Thanx in advance.

0
source share
1 answer

, Flurl . , . ( #, , VB.)

public static Task<HttpResponseMessage> PutFileAsync(this FlurlClient client, string filepath) 
{
    var data = File.ReadAllBytes(filepath);
    var content = new ByteArrayContent(data);
    content.Headers.Add("Content-Type", "application/octet-stream");
    content.Headers.Add("Content-Length", data.Length);
    return client.SendAsync(HttpMethod.Put, content: content);
}

FlurlClient , FlurlClient, , , string Url, :

public static Task<HttpResponseMessage> PutFileAsync(this Url url, string filepath)
{
    return new FlurlClient(url).PutFileAsync(filepath);
}

public static Task<HttpResponseMessage> PutFileAsync(this string url, string filepath)
{
    return new FlurlClient(url).PutFileAsync(filepath);
}

, Flurl:

await uploadurl.PutFileAsync(filepath)
+2

Source: https://habr.com/ru/post/1679112/


All Articles