locked
How to make a web service request to upload .csv file? Converting curl to c# RRS feed

  • Question

  • User-575144709 posted

    Hello,

    API Document: https://api.qualtrics.com/reference#start-import

    Here is the curl code:

    curl -X POST -H 'X-API-TOKEN: yourapitoken' \
    		-H 'Content-Type: text/csv; charset=UTF-8' \
    		--data-binary '@example.csv' \
    		'https://yourdatacenterid.qualtrics.com/API/v3/surveys/SV_123456/import-responses'




    The below codes runs without any error but status is failed when I check progress status of the file upload.

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://yourdatacenterid.qualtrics.com/API/v3/surveys/surveyid/import-responses");
    request.Method = "POST";
    request.ContentType = "text/csv; charset=UTF-8";
    request.Headers.Add("X-API-TOKEN", "xxxxxxxxxxx");

    //ByteArrayContent bytes = new ByteArrayContent(File.ReadAllBytes(@"H:\Qualtrics Project Management\API Import Testing\RealSurveyProject_API_Import.csv"));
    Stream requestStream = request.GetRequestStream();
    //using (var streamWriter = new Stream(request.GetRequestStream()))
    //{
    using (FileStream fileStream = new FileStream(@"H:\Qualtrics Project Management\API Import Testing\RealSurveyProject_API_Import.csv", FileMode.Open, FileAccess.Read))
    {
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
    requestStream.Write(buffer, 0, bytesRead);
    }
    fileStream.Close();
    }
    //streamWriter.Write(bytes);
    //streamWriter.Flush();
    requestStream.Close();

    var httpResponse = (HttpWebResponse)request.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
    var result = streamReader.ReadToEnd();
    Console.WriteLine(result.ToString());
    }
    //}
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message.ToString());
    }

     

    Friday, April 26, 2019 12:36 PM

All replies

  • User36583972 posted

    API Document: https://api.qualtrics.com/reference#start-import

    Here is the curl code:

    curl -X POST -H 'X-API-TOKEN: yourapitoken' \
    		-H 'Content-Type: text/csv; charset=UTF-8' \
    		--data-binary '@example.csv' \
    		'https://yourdatacenterid.qualtrics.com/API/v3/surveys/SV_123456/import-responses'




    The below codes runs without any error but status is failed when I check progress status of the file upload.

    have you tried F12 Nework, F12 Console? I'm trying to suggest that rather than stopping at seeing that "tatus is failed", it should be easier to fix the issue if you look at what actually happens (F12 console for possible error messages, F12 network to inspect the http query and the response).


    Best Regards

    Yong Lu

    Monday, April 29, 2019 9:55 AM