How to create creative through API by c# httpclient?

41 views
Skip to first unread message

zion...@gmail.com

unread,
Nov 14, 2018, 11:38:34 PM11/14/18
to PocketMath API
I'm trying to create a new creative through API using C# and httpclient.
But don't know how to pass parameters and file at the same time using MultipartFormDataContent.
Is there any example for this?
My code looks like this, but it doesnt work and get internal error. (No response content to say where it was wrong).

            var content = new MultipartFormDataContent();

            // parameters without image_file
            var serializedString = JsonConvert.SerializeObject(newCreative,
                Formatting.None, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                }
            );

            var sc = new StringContent(serializedString, Encoding.UTF8, "application/json");
            content.Add(sc);

            // for image_file
            using (var memoryStream = new MemoryStream())
            {
                newCreative.ImageFile.CopyTo(memoryStream);
                var fileContent = new ByteArrayContent(memoryStream.ToArray());
                fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    Name = "image_file",
                    FileName = Path.GetFullPath(newCreative.ImageFile.FileName)
                };
                fileContent.Headers.Add("Content-Type", "image/jpeg");
                content.Add(fileContent, "image_file");
            }

            try
            {
                var client = new HttpClient()
                {
                    BaseAddress = new Uri("https://api.pocketmath.com/v3/")
                };

                client.DefaultRequestHeaders.Add("Authorization", apiKey);

                var path = "creatives.json";

                var responseResult = await client.PostAsync(path, content);

                if (responseResult.IsSuccessStatusCode)
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                return false;
            }


If I use content like this, will give an error response, but it says can't find the type.
(Seems the format to wrap the stringcontent into the MultipartFormDataContent is wrong).

var content = new MultipartFormDataContent();
content.Add(new StringContent("banner"),"type");
//use this to call the httpclient...

Andi Susanto

unread,
Nov 15, 2018, 12:55:02 AM11/15/18
to PocketMath API
Hi Zion,

May we ask you to create a fiddle in https://dotnetfiddle.net/ and share the link with us so that we can help you with the script?

zion...@gmail.com

unread,
Nov 15, 2018, 4:56:27 AM11/15/18
to PocketMath API

Andi Susanto

unread,
Nov 16, 2018, 1:25:01 AM11/16/18
to PocketMath API
Hi,

This is the fiddle that we come up with, translated from POSTMAN: https://dotnetfiddle.net/zWWLjr, with the most important snippet highlighted here:

request.AddFile("image_file", filePath);

Unfortunately, dotnetfiddle does not allow us to save the downloaded image because of the permission setting. So please try attaching the file using RestSharp library and let us know if it works for you.

Cheers!

zion...@gmail.com

unread,
Nov 16, 2018, 3:55:25 AM11/16/18
to PocketMath API
Thanks for your information.
I think restSharp can solve the problem. ( But we are not going to use restSharp package maybe).

I was trying how to fix it myself. and got the solution.
I made this sample to show how it works.

Thank you!

Andi Susanto

unread,
Nov 16, 2018, 4:29:59 AM11/16/18
to PocketMath API
Awesome, thanks for the update!
Reply all
Reply to author
Forward
0 new messages