1

I am currently trying to make an api call in c# using a MultiPartFormDataContent but I keep getting the following error:

"Response: {"statusCode":400,"error":"Bad Request","message":""Image file" must be of type object","validation":{"source":"payload","keys":["images"]}}"

This is my Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Example
{
    public class Test
    {
        public static void Main(string[] args)
        {
            Task<string> test = testFunction();
            Console.WriteLine("Result: " + test.Result);
        }

        public static async Task<string> testFunction()
        {
            const string file = "C:\\ExamplePath\\image_1.jpeg";
            const string URL = "https://example-api?api-key=example-key";

            string boundary = "---8d0f01e6b3b5dafaaadaad";
            MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);

            var streamContent = new StreamContent(File.Open(file, FileMode.Open));
            var stringContent = new StringContent("flower");

            multipartContent.Add(stringContent, "organs");
            multipartContent.Add(streamContent, "images");

            try
            {
                HttpClient httpClient = new HttpClient();
                HttpResponseMessage response = await httpClient.PostAsync(URL, multipartContent);

                Console.WriteLine("Response: " + await response.Content.ReadAsStringAsync());

                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("IN METHIOD: " + content);
                    return content;
                }
                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }
}

It's obviously a problem with how I am trying to do the api call but I don't know how to do it with an object instead like mentioned the error message.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
gdzugsauz
  • 11
  • 1
  • 2
  • It seems like you're talking to it as it sent you a response back. Put a breakpoint on the response, and copy the JSON and put it through a JSON Validator. – Andrew Reese Jun 09 '21 at 19:19

1 Answers1

1

This link has some good examples and where I actually got my code snippet from below.

Here's a basic example of using MultiFormDataContent:

HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();

form.Add(new StringContent(username), "username");
form.Add(new StringContent(useremail), "email");
form.Add(new StringContent(password), "password");            
form.Add(new ByteArrayContent(file_bytes, 0, file_bytes.Length), "profile_pic", "hello1.jpg");
HttpResponseMessage response = await httpClient.PostAsync("PostUrl", form);

response.EnsureSuccessStatusCode();
httpClient.Dispose();
string sd = response.Content.ReadAsStringAsync().Result;

I hope this helps or points you in the right direction.

Andrew Reese
  • 854
  • 1
  • 11
  • 27
  • 1
    Thank you very much for your response! Unfortunetly I have already tried multiple examples (including this one) without any success. I am currently trying to find the json to put it through a JSON Validator as you recommended in your comment. :) – gdzugsauz Jun 09 '21 at 19:32
  • Try putting a breakpoint on the response and step over once and hover over the response. It should show it. – Andrew Reese Jun 09 '21 at 19:34
  • This is what I get when I hover over the response after putting a breakpoint: {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers: { Server: nginx/1.14.0 Server: (Ubuntu) Date: Wed, 09 Jun 2021 19:35:34 GMT Connection: keep-alive api-version: 2 Cache-Control: no-cache Content-Type: application/json; charset=utf-8 Content-Length: 142 }} – gdzugsauz Jun 09 '21 at 19:36
  • @gdzugsauz Were you able to get the response back? – Andrew Reese Jun 30 '21 at 20:34