locked
convert Node.JS webservice call to C# RRS feed

  • Question

  • User-235682625 posted

    Hi Folks,

    I have an Node.JS script which is making a webservice call the script works great , i am looking to convert this Node.JS script to C# webservice call.

    Can someone please help. Thanks in advance.

    var request = require('request');
    var fs = require('fs');
    var url = "https://yoururl/API/v3/responseimports";
    var surveyId = "SV_ePswPpv4rPD77fL";
    var token="AXXXXXXXXXXXXXZZZZ";
    var formData={
    	surveyId:surveyId,
    	file: fs.createReadStream("test.csv")
    };
    
    var options = {
    	headers:{
    		'content-type':'multipart/form-data',
    		'X-API-TOKEN':token
    	},
    	method:"POST",
    	url:url,
    	formData: formData
    };
    
    request(options, function(error,response,body){
    	if(error){
    		console.log("Error" +error);
    	}
    	else{
    		console.log(response.body);
    		console.log("webservice call completed");
    		console.log(response.statusCode);
    
    	}
    		
    });

    Friday, June 23, 2017 3:45 PM

All replies

  • User1068175894 posted

    Try this code:

        string boundary = "12345";
    	var filename = "test.csv";
    	var paramFileStream = File.Open(fileName, FileMode.Open, FileAccess.Read);
    	
    	HttpContent stringContent = new StringContent("SV_ePswPpv4rPD77fL");
        HttpContent fileStreamContent = new StreamContent(paramFileStream);
    	streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
    	streamContent.Headers.ContentDisposition.Name = "\"file\"";
    	streamContent.Headers.ContentDisposition.FileName = "\"" + fileName + "\"";	
    	
        using (var client = new HttpClient())
        using (var formData = new MultipartFormDataContent(boundary))
        {
            formData.Add(stringContent, "surveyId");
    		formData.Headers.Remove("Content-Type");
            formData.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
            formData.Add(fileStreamContent, "file");
    		
            var response = client.PostAsync(actionUrl, formData).Result;
    		response.EnsureSuccessStatusCode();
    		fileStreamContent.Dispose();
    		paramFileStream.Dispose();
        }

    Friday, June 23, 2017 4:52 PM
  • User-235682625 posted

    this will not work i am using .NET version 4.0 , i am not able to use HttpContent class.

    Friday, June 23, 2017 5:42 PM
  • User2053451246 posted

    this will not work i am using .NET version 4.0 , i am not able to use HttpContent class.

    You can use HttpContent in 4.0 by adding a NuGet package:

    https://www.nuget.org/Packages/Microsoft.Net.Http

    Friday, June 23, 2017 6:57 PM
  • User-235682625 posted

    Still needs 4.5 to install that package since it uses libraries from 4.5

    Friday, June 23, 2017 7:56 PM
  • User991499041 posted

    Hi Renu,

    You can refer below code snippets.

    main()
    {
        HttpWebRequest request;
        HttpWebResponse response = null;
        string url = "*****URL*****";
        string path = @"..\test.csv";
        Random rand = new Random();
        string contentType = "text/csv";
        byte[] header = RequestHeader(boundary,path,nameValue, contentType);
        request = (HttpWebRequest)WebRequest.Create(url);
        RequestParameters(header, path, boundary);
        response = (HttpWebResponse)request.GetResponse();
    }
    private byte[] RequestHeader(string boundary, string path, string nameValue, string contentType)
    {
        return System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\nContent-Dis-data; name=\"" + nameValue + "\"; filename=\"" + System.IO.Path.GetFileName(path) + "\"\r\nContent-Type:" + contentType + "\r\n\r\n");
    }
    private void RequestParameters(byte[] header, string path, string boundary)
    {
        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        request.Method = "post";
        request.KeepAlive = true;
        request.ContentType = "multipart/form-data" + boundary;
        data_stream = request.GetRequestStream();
        data_stream.Write(header, 0, header.Length);
        byte[] file_bytes = System.IO.File.ReadAllBytes(path);
        data_stream.Write(file_bytes, 0, file_bytes.Length);
        data_stream.Write(trailer, 0, trailer.Length);
        data_stream.Close();
    }

    Regards,

    zxj

    Tuesday, June 27, 2017 7:20 AM