How to send PDF to Webservice - Asp . NET

Asked

Viewed 216 times

1

I am having trouble sending a PDF to a Web Service. In case, the documentation is this way: inserir a descrição da imagem aqui

then I made the following code:

private async void EnviarDocumentoWhats(string pdf)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://url");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "token");

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("remotejid", "ID"),
                new KeyValuePair<string, string>("content", pdf),
                new KeyValuePair<string, string>("caption", "valor")
            });

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var result = await client.PostAsync("/message/document", content);

            string resultContent = await result.Content.ReadAsStringAsync();

        }
    }

When running the application I get the following in the resultcontent:

"{\"Code\":400,\"Type\":\"error\",\"Message\":\"Document title is empty, or wrong size.\"}\n"

It says that the document is empty or wrong size, but I don’t know how to resolve this issue. Checking again the documentation I saw is part: inserir a descrição da imagem aqui

Does anyone know how I can resolve this situation? The error as the message itself says is with respect to how this document is being sent, does anyone have a hint or hint of the correct way to send? From now on I thank anyone who can help.

Note: The token and ID are working correctly, I have tested sending only messages using another method and is working.

  • Apparently the API is expecting a blob or byte[] converted into string in the content property, just as there is no indication that you should change the content-type in the header...

  • @Leandroangelo, thank you for answering. Look, I made change by converting the document to byte[] and then to string, but it still doesn’t work, I made the change in the question, could you take a look please? thanks Obs: the error you are now giving is: "{"Code":400,"Type":"error","Message":"error Reading sent file"} n"

  • You keep creating a Multipart... the visible part of your documentation awaits a post application/json... Information about this specification is missing... where it is written that you should convert the content to Base64?

  • @Leandroangelo, I made another modification, not Multipart. This method I have now put is the same as I use to send "text only", I just added an extra value to the content, but it still doesn’t work, I converted the document to Base64 and normal, but it keeps giving error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.