I am Getting the error "(400) Bad Request" on Calling ConvertApi web to Pdf Api.
Asked
Active
Viewed 598 times
0
-
1Welcome to SO. The way you have state your ploblem is pretty difficult to get any help. You refer to things that are not visible to the reader of your post. What is ConvertAPI? Is it an API you have written or an API that has been written from somebody else? Which is the method of this API you call and what is a proper request/response. HTTP status code 400 means that your request has something that is not expected from this API, in order to respond properly. Usually there are more informations regarding the false items in the request. Thanks – Christos Oct 20 '17 at 18:24
-
A 400 BAD REQUEST means you are sending a request to the server that is malformed. We can't tell you any more than that, and you could have googled that yourself. – Oct 20 '17 at 18:42
-
ConvertAPI is a third party Api which i was consuming it in my project. – Avinash Oct 20 '17 at 19:08
-
What does response body says? You just telling us status code which is not enough to narrow down the problem. Usually Rest Api sends body along with response code. – Tomas Nov 20 '17 at 09:23
2 Answers
0
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var response = client.UploadString("https://v2.convertapi.com/web/to/pdf?secret=" + Secret + "&Url=" + value + "&ConversionDelay=" + ConversionDelay, "");
var ocontent3 = JsonConvert.DeserializeObject<FileList>(response);
byte[] result = ocontent3.Files[0].FileData;
}

Avinash
- 19
- 3
-
Finally, I found my answer with the above solution. This may help you.. – Avinash Oct 24 '17 at 06:55
0
ConvertAPI and many other Rest Api which operates with binary data support multipart or application/octet-stream(binary file) response, it is better to use binary response instead of json(textual) in C#. It will be faster - the response body will be smaller, download time shorter and no need to decode binary data from JSON Base64.
So the code could be
const string secret = "<YourSecret>";
const string url = "http://www.google.com";
const int conversionDelay = 1;
const string fileToSave = @"C:\Projects\_temp\test1.pdf";
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
var response = new byte[] { };
try
{
response = client.UploadValues("https://v2.convertapi.com/web/to/pdf?secret=" + secret, "POST", new NameValueCollection
{
{ "Url", url },
{ "ConversionDelay", conversionDelay.ToString() }
});
}
catch (WebException e)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
}
if (response != null)
File.WriteAllBytes(fileToSave, response);
}

Tomas
- 17,551
- 43
- 152
- 257