简体   繁体   中英

Post request upload PDF file C#

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();
            }
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM