Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curl upload not working when upload this #10

Closed
jagadeesh1492 opened this issue Apr 17, 2015 · 3 comments
Closed

curl upload not working when upload this #10

jagadeesh1492 opened this issue Apr 17, 2015 · 3 comments

Comments

@jagadeesh1492
Copy link

When i post a curl url i was facing this strange issue

message
{"Object contains non-primitive or non-blittable data."}
at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object value, GCHandleType type)
at System.Runtime.InteropServices.GCHandle.Alloc(Object value, GCHandleType type)
at CurlSharp.CurlEasy.getHandle(Object obj) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 1526
at CurlSharp.CurlEasy.setReadData(Object data) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 252
at CurlSharp.CurlEasy.set_ReadData(Object value) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 330
at Upload.Upload.Main(String[] args) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\Samples\Upload\Upload.cs:line 36

Curl command is

curl -i -X POST -H "Content-Type: multipart/form-data" -H "Authorization: SIMPLE Y2F0Y2h0aGVtb21lbjpjYXRjaHRoZW1vbWVudDExMQ==" -F "card={"text":"sometext", "userName":"%USERNAME%", "screenName":"screenName", "avatarUrl":"https://www.google.com/images/srpr/logo11w.png\"};type=application/json" -F "pic=@c:\users\my\desktop\download.jpg" "https://socialwall.cvent.com/livestream/api/event/395b4759-8b93-4fb1-b33b-387c3928c1ca/socialpost" -v

below code is my implementation of above curl command

public static void Main(String[] args)
{
try
{

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "download.jpg");
            Curl.GlobalInit(CurlInitFlag.All);

            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string _cred = string.Format("{0} {1}", "SIMPLE", "Y2F0Y2h0aGVtb21lbjpjYXRjaHRoZW1vbWVudDExMQ==");
                string url = "https://socialwall.cvent.com/livestream/api/event/395b4759-8b93-4fb1-b33b-387c3928c1ca/socialpost";
                var card = new Card
        {
            text = "Ohmygod",
            userName = "CatchTheMoment",
            screenName = "Socialpost",
            avatarUrl = "https://www.google.com/images/srpr/logo11w.png"
        };
        var jsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(card);
                using (var easy = new CurlEasy())
                {
                    easy.ReadFunction = OnReadData;
                    easy.ReadData = fs;
                    var headers = new CurlSlist();
                    headers.Append("Content-Type: multipart/form-data");
                    headers.Append("Accept: */*");
                    headers.Append("user-agent:Mozilla/4.0");
                    headers.Append("Authorization0:"+_cred);
                    headers.Append("Except: 100-continue");

                    easy.HttpHeader = headers;
                    easy.DebugFunction = OnDebug;
                    easy.SetOpt(CurlOption.Verbose, true);
                    easy.SetOpt(CurlOption.Header, true);
                    easy.ProgressFunction = OnProgress;
                    easy.Url = url;
                    easy.Upload = true;
                    easy.InfileSize = fs.Length;
                    easy.PostFields = "card="+jsonstring;
                    easy.PostFieldSize = jsonstring.Length;
                    easy.Perform();
                    Console.ReadLine();
                }
            }

            Curl.GlobalCleanup();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
    {
        var fs = (FileStream) extraData;
        return fs.Read(buf, 0, size*nmemb);
    }

    public static void OnDebug(CurlInfoType infoType, String msg, Object extraData)
    {
        Console.WriteLine(msg);
    }

    public static Int32 OnProgress(Object extraData, Double dlTotal,
        Double dlNow, Double ulTotal, Double ulNow)
    {
        Console.WriteLine("Progress: {0} {1} {2} {3}",
            dlTotal, dlNow, ulTotal, ulNow);
        return 0; // standard return from PROGRESSFUNCTION
    }
    public class Card
    {

        public string text { get; set; }
        public string userName { get; set; }
        public string screenName { get; set; }
        public string avatarUrl { get; set; }
    }
@bapzangbo
Copy link

I have same Problem. have You had solution to fix that ? @jagadeesh1492

@Riyas-precise
Copy link

i was moving to another approach by executing curl command in curl exe..its worked now.

Jagadeesh Govindaraj

@masroore masroore closed this as completed Jun 2, 2016
@ryysoft
Copy link

ryysoft commented Aug 21, 2020

i meet same problem,do me a favour,thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants