1

I am using php REST API and C# to post an image along with access token by using RestSharp, sending image file and access token as parameters but i unable to achieve with this below sample program

 private static void postPrescription(string ext, string mime, string token)
    {
        var restClient = new RestClient("http://api.xxy.in/v1/docsupload");
        restClient.AddHandler("application/octet-stream", new RestSharp.Deserializers.JsonDeserializer());
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("client-Identifier", "192.168.1.24");
        request.AddHeader("client-Platform", "kiosk");
        request.AddHeader("client-Version", "2.00");
        request.AddHeader("client-Type", "kiosk");
        request.AddHeader("Accept", "application/json");

        Dictionary<string, object> dict = new Dictionary<string, object>();
        dict.Add("FILE_EXT", ext);
        dict.Add("FILE_MIME_TYPE", mime);
        byte[] imageBytes;
        using (FileStream fs = File.Open(@"C:\Prescriptions\Presc_1_10_2015_17_19_17.jpeg", FileMode.Open))
        {
            imageBytes = new BinaryReader(fs).ReadBytes((int)fs.Length);
        }
        string image = Convert.ToBase64String(imageBytes);
        dict.Add("IMAGE_DATA", image);
        byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dict));
        request.AddParameter("access_token", token);
        request.AddParameter("userfile", data);
        var response = restClient.Execute(request);
        JavaScriptSerializer json = new JavaScriptSerializer();
        Dictionary<string, object> dict1 = json.Deserialize<Dictionary<string, object>>(response.Content);

    }  

While trying with above snippet i got a response as "{\"response\":400,\"message\":\"File not supported\"}"

After this I tried using HTTP POST method but not succeeded, i got http response as "{\"response\":401,\"message\":\"You have been logged out to protect your privacy. Please log back in.\"}"

This is my second snippet,

  public static void prescriptionPost(string token)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.xxy.in/v1/docsupload");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";
        httpWebRequestHeaders(httpWebRequest);
        httpWebRequest.ContentType = "application/octet-stream";
        byte[] imageBytes;
        using (FileStream fs = File.Open(@"C:\Prescriptions\Presc_1_10_2015_17_19_17.jpeg", FileMode.Open))
        {
            imageBytes = new BinaryReader(fs).ReadBytes((int)fs.Length);
        }
        string image = Convert.ToBase64String(imageBytes);
        byte[] data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(image));
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            pres ps = new pres
            {
                access_token = token,
                userfile = image
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(ps, Newtonsoft.Json.Formatting.Indented);
            streamWriter.Write(json);
        }


        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            StringBuilder output = new StringBuilder();
            var result = streamReader.ReadToEnd();
            var serializer = new JavaScriptSerializer();
            var myobj = serializer.Deserialize<ObtainToken>(result);
        }

       // example deserializedProduct = Newtonsoft.Json.JsonConvert.DeserializeObject<example>(json);
    }

 internal class pres
{
    public string access_token { get; set; }
    public string userfile { get; set; }
}

I dont know whether my snippets are correct or there was a problem exists in RestApi Can anyone suggest me that how can i achieve this.

1
  • Does it work when you upload images in browser? If so, use a debugging proxy like Fiddler to see how your requests are different from browser requests. Commented Oct 3, 2015 at 14:44

1 Answer 1

1
using RestSharp;

byte[] imgdata = ImageFileToByteArray(@"D:\SampleIMG.jpg");
RestRequest request = new RestRequest("URL", Method.POST);

request.AddParameter("secret_key", "secret_key_data");
request.AddParameter("Name_Key","Mahesh");
request.AddFile("imageKey", imgdata, "SampleIMG.jpg", "image/jpeg");
RestClient restClient = new RestClient();
IRestResponse response = restClient.Execute(request);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.