[Solved] Error – Couldn’t connect to server

February 12th, 2019 / by api2pdf /

Intro

With our service being as cheap as it is, we deal with all sorts of developers. Some are experienced and ask us very complicated questions about our tech stack, while other developers are quite new to programming. We try to help everyone just the same. The issue described in this blog post seems to be coming up more and more and affects those new to programming, so hopefully this helps.

Couldn’t Connect to Server

Unexpected error: An error occurred while sending the request. Couldn't connect to server, (StackTrace: at System.Net.HttpWebRequest.GetResponse() at System.Net.WebClient.GetWebResponse(WebRequest request) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadData(Uri address) at InvokePdfMerge.PdfMerger.Merge(IReadOnlyCollection`1 pdfUrls, String fileName, Boolean inlinePdf, Double& inputMb, Double& outputMb) in C:\git\Api2PdfLambda\Api2PdfLambda\InvokePdfMerge\PdfMerger.cs:line 22 at InvokePdfMerge.PdfMergeFunction.Handler(MergeRequest input) in C:\git\Api2PdfLambda\Api2PdfLambda\InvokePdfMerge\PdfMergeFunction.cs:line 24)

This is an example error of what you might be getting. Developers tend to just send me this error message with nothing more. But it is difficult to help you if you do not provide context. Often times the developers write in saying the API is broken and required URGENT FIX!!!! Slow down now, the API is just fine.

To understand what is happening if you use API2PDF’s URL to PDF or Merge PDFs, is that you are providing a URL to an existing page or PDF on the web. API2PDF is attempting to consume the PDF at that URL and then process it. The error is occurring because it cannot connect to the URL you provided.

Debugging

The next step I tell them is to follow our guide in how to effectively get support here. More experienced developers may find their issue obvious after going through that exercise. If not, you may send a code snippet like so:

                var links_to_pdfs = new List() {
                    "http://localhost:5000/1.pdf",
                    "http://localhost:5000/2.pdf" 
                };

                var apiResponse = a2pClient.Merge(links_to_pdfs);

Do you see the problem yet?

Fixing the Issue

localhost is your development machine. If you send a url to a PDF to API2PDF that is localhost, there is nothing API2PDF can do. Localhost is your own machine, and API2PDF lives in the cloud on AWS Lambda, so it has no access to your file. That is why when you send urls to API2PDF, either with the URL to PDF endpoint or the Merge PDFs endpoints, the urls have to be live and accessible on the web at a real url.

This is not an API2PDF specific problem. If you need to run code on your development machine but test against an API or webhook service, you need to provide a mechanism for those services to access your development machine with a valid url.

We recommend looking into either of the following:

Both are free and allow you to establish a secure tunnel to your localhost machine. You will get a legitimate url that you can use to send to services like API2PDF.

Hope that helps.

Comments are closed.