0

I am sending post request with pdf file attached example.pdf - this file is added to project in Visual Studio as "content". Problem is that I am receiving 400 Bad request. API server is receiving (IFormFile uploadedFile) but in my case uploadedFile is null. Authorization is good, url, headers also. I checked it via postman and it is working properly. requestbody in debug mode is '{byte[63933]}' How to solve this in C#?

string pathToPdfFile = "Scenarios\DefaultScenario\example.pdf";
byte[] requestBody = File.ReadAllBytes(pathToPdfFile);     

public static string PostRequestUploadFile(string url, Dictionary<string, string> headersDictionary, byte[] requestbody)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            if (headersDictionary != null)
            {
                foreach (KeyValuePair<string, string> entry in headersDictionary)
                {
                    request.Headers.Add(entry.Key, entry.Value);
                }
            }
            request.ContentType = "application/pdf";
            Stream dataStream = request.GetRequestStream();
            byte[] byteArray = requestbody;
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                return Ex.ToString();
            }
        }
YoungEddie
  • 405
  • 7
  • 26
  • Bad request means that the server do not like what you are doing. Can be some restriction of the server. Can you share what server/service is that ? Additionally add a `using` on your `dataStream` (you are already doing it for `response`,`stream` and `reader`) – Rafael Jun 07 '19 at 16:03
  • Another comment: As it is a new implementation, I would suggest not using `HttpWebRequest` as it is now legacy. Read more about it [here](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – Rafael Jun 07 '19 at 16:13
  • There are no restrictions at server. When I made similar post request using postman, I add pdf file from desktop disk in 'body' as 'form-data'. - it works. Pdf file is saved at MySql Database (AWS). I know I should consider not to use HttpWebRequest. – YoungEddie Jun 07 '19 at 16:27
  • You should add the ContentLength header `request.ContentLength = byteArray .Length;` so the server know how much you will send – Rafael Jun 10 '19 at 12:47

1 Answers1

0

I have made some changes I added the Content Length header

You may have to change application/pdf to application/x-www-form-urlencoded Finally, I don't know which parameters you are sending in headersDictionary, but may be missing the form 'file' field name

var request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "POST"; // Consider using WebRequestMethods.Http.Post instead of "POST"
if (headersDictionary != null){
    foreach (KeyValuePair<string, string> entry in headersDictionary){
        request.Headers.Add(entry.Key, entry.Value);
    }
}

request.ContentType = "application/pdf";
// Dependending on your server, you may have to change
// to request.ContentType = "application/x-www-form-urlencoded";

byte[] byteArray = requestbody; // I don't know why you create a new variable here
request.ContentLength = byteArray.Length;

using (var dataStream = request.GetRequestStream()){
    dataStream.Write(byteArray, 0, byteArray.Length);
}

using(var response = (HttpWebResponse)request.GetResponse()){
    using(var reader  = new StreamReader(response.GetResponseStream())){
        return reader.ReadToEnd();
    }
}

In my tests using this I have to use request.ContentType = "application/x-www-form-urlencoded" instead of PDF (I can't mock your entire setup)

As I don't have the server you are trying to send this and don't have the parameters I can't test this in your environment

For future reference, HttpWebRequest is a Legacy (obsolete) implementation that should be avoided and new implementations should use HttpClient read more

Rafael
  • 345
  • 3
  • 16