0

I'm trying to post a file in a json object to an external rest api over https. I have confirmed the json object is formatted correctly, do I have to do anything special to post to a rest api over https? I'm using the answer found over here as a guide: How to post JSON to the server?

 private static void PostDatatoFTP(string FileName,
     string fileString, string centerCode, string fileType) {
        try {

            byte[] plainTextBytes = Encoding.ASCII.GetBytes(fileString);
            string base64File = Convert.ToBase64String(plainTextBytes);

            FileInfo fileInfo = new FileInfo {
                FileData = base64File, 
                FileName = FileName, 
                FileType = fileType, 
                FileVersion = _fileVersion
            };



            FileInfo[] transmitFileInfo = new FileInfo[1];
            transmitFileInfo[0] = fileInfo;


            Json jsonObject = new Json {
                RequestType = _RequestType,
                APIVersion = _apiVersion,
                SubmissionId = Guid.NewGuid().ToString(),
                UserId = _ftpUsername,
                Password = _ftpPassword,
                Vendor = _vendor,
                CenterCode = centerCode,
                FileInfo = transmitFileInfo
            };

            var json = JsonConvert.SerializeObject(jsonObject);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_uploadPath);
            request.Method = "POST";
            request.ContentType = "application/json";

            using (var streamWriter = new StreamWriter(request.GetRequestStream())) {
                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException())) {
                var result = streamReader.ReadToEnd();
                Console.WriteLine(result);

            }


        }
        catch (WebException e) {
            Console.WriteLine(e.Message);
            String status = ((HttpWebResponse)e.Response).StatusDescription;
            Console.WriteLine(status);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
Mykal73
  • 478
  • 4
  • 6
  • 18

0 Answers0