C# post request to assets

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [x] Documentation issue or request

Current behavior

Hello, sorry for another ticket.
I am playing with squidex and it seems really great.
Api works very nicely but I am unable to understand how should look post request to assets in c#.
While I found some examples in typescript here on forum I dont know how to replicate in c# maybe because i have very limited experience.

static async void PostRequestMedia(HttpClient client, string Bearertoken)
        {

            HttpClient downloadClient = new HttpClient();
            HttpResponseMessage downloadResponse = await downloadClient.GetAsync(
                "http://wallpapers.net/web/wallpapers/beautiful-water-house-nature/1080x608.jpg");
            byte[] content = await downloadResponse.Content.ReadAsByteArrayAsync();
            
            var formData = new MultipartFormDataContent();
            formData.Add(new ByteArrayContent(content, 0, content.Length));
            formData.Add(new StringContent(
                JsonSerializer.Serialize(new
                {
                    mimeType = "image/jpg"
                }),
    Encoding.UTF8,
    "application/json"));



            client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Bearertoken);

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
            string param = "?Duplicate=true";
            HttpResponseMessage response = await client.PostAsync(
                "api/apps/kviiik/assets" + param,
                formData);

            response.EnsureSuccessStatusCode();
        }

This is what i was able to build and here I am basically stack. Still getting 400 bad request. What I am doing is basically that I am downloading image from url and uploading it to squidex assets. Can I get please help on this?

Expected behavior

Minimal reproduction of the problem

Environment

App Name:

  • [x] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version: [VERSION]

Browser:

  • [x] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Others:

I would just use the SDK: https://www.nuget.org/packages/Squidex.ClientLibrary

Or search for PostAssetAsync in this file (second result):
https://raw.githubusercontent.com/Squidex/squidex-samples/471e7111e9b63dcf7cf79984cf5a4c668b1750a6/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Management/Generated.cs

yeah thank you great resources :slight_smile:

1 Like