0

I have trying to posting a pdf file to rest api and I get a 500 internal server error. The code works for a word doc and its also works in debug mode in a visual studio 2015 but fails in production. At this point I am not sure if its a code issue. Help, don't have much hair left.

I have tried both IE and chrome no difference. I have tried other document file types does are fine, but PDF. Do I have to do something different for PDF other then the content type?

private bool Request_login_mydgsi_ca(out HttpWebResponse response)
{
    response = null;

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://login.mydgsi.ca/WebAPI/Attachment?aboutType=Candidate&referenceID="+Candidate_id.Trim()+"&attachmentTypeID=Resume&Name=Resume&expirationDate=1900/01/01&Note=Candidate%20Resume");

        request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");

        string client_id = ConfigurationManager.AppSettings["client_id"].ToString();
        string secret_id = ConfigurationManager.AppSettings["secret_id"].ToString();

        ERApplyForJobs ePosition = new ERApplyForJobs(client_id, secret_id, "");
        string tokenAPI = ConfigurationManager.AppSettings["APIToken"].ToString();
        string secruityToken = ePosition.getSecurityToken(tokenAPI);
        request.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + secruityToken );

        request.ContentType = "multipart/form-data; boundary=---224842443399224314204538";
        request.KeepAlive = true;
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = true;
        string contentType = MimeMapping.GetMimeMapping(fileName);
        string body = @"---224842443399224314204538
            Content-Disposition: form-data; name=""""; filename=""" + fileName + @"""
            Content-Type: " + contentType + @"<!>" + fullFileName + @"<!>---224842443399224314204538--";


        WriteMultipartBodyToRequest(request, body);
        response = (HttpWebResponse)request.GetResponse();


    }
    catch (WebException e)
    {
        response = (HttpWebResponse)e.Response;

    }
    catch (Exception)
    {
        if (response != null) response.Close();
            return false;
    }

    return true;
}
Jason
  • 1
  • 1
  • HTTP 500 means it's a bug in the server's code, not your client code. – Dai May 17 '18 at 20:42
  • Verify IIS file upload size and your file size – Mate May 17 '18 at 20:42
  • I do see a bug though: you need to "rewind" your `MemoryStream ms` back to `Position = 0` so `WriteTo` will start from the beginning. – Dai May 17 '18 at 20:42
  • Your code is not generating a MIME Multipart message correctly. See this QA to see how to generating a multipart message correctly: https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – Dai May 17 '18 at 20:43
  • A word document of roughly the same size words fine. I keep going back to different between PDF and Word. I set the content type to the resulf of MimeMapping.GetMimeMapping(fileName); which is "application/pdf". – Jason May 17 '18 at 21:06
  • Do us both a favor and Please only include code relevant to the question, it makes your question more clear and more people will be willing to answer it – johnny 5 May 17 '18 at 21:31
  • One thing that jumps out at me: `request.Headers["Accept-Encoding"] = "pdf";` That value doesn't make sense to me and seems like something that may be likely to cause a server to throw an unexpected 500. From MDN: "The Accept-Encoding request HTTP header advertises which content encoding, usually a compression algorithm, the client is able to understand. Using content negotiation, the server selects one of the proposals, uses it and informs the client of its choice with the Content-Encoding response header." – p e p May 18 '18 at 15:44
  • Thanks, I have removed that line. I have been trying different thing forgot that i left that in. Still not working. It got something to differences with PDF i just don't know what. – Jason May 18 '18 at 17:27

1 Answers1

0

The issue was related to the file upload and not being able to get the full path. The weird thing is it worked in word but not for PDF.

I not sure why it worked for word docs and failed for PDFs. In my way i thinking it should have failed for both.

Jason
  • 1
  • 1