TheAlphaSchoolSystemPTYLTD / api-introduction

A basic introduction to the TASS API's.
16 stars 10 forks source link

Request URL is too long when attaching a document in an invoice #19

Closed geejavier0422 closed 6 months ago

geejavier0422 commented 7 months ago

The problem arises when converting the binary data of a PDF file to a base64 string, which results in a long string. This string is then encoded into JSON for the encryption of tokens, leading to an even longer web service URL. As a consequence, the URL length exceeds the limitations imposed on URLs, causing problems in the process.

Are there any alternatives where parameters are in the body instead of adding it in the url?

leonseremelis-tass commented 7 months ago

Hi, thanks for your query, can you confirm the API that you are using and the size of the URL string that you are trying to pass?

Are you hosting TASS on your own servers or do you have a hosted instance?

geejavier0422 commented 7 months ago

Hi!

The API endpoint i am using is 'SetPOInvoice' and the size of the URL is 600+ characters. I am using a hosted instance.

I am trying to build a json of PO Invoice and I am pulling the data from an invoice in filebound. When we are including the attachment, I used Convert.ToBase64String(doc.BinaryData);

the document from filebound is a pdf with a size of 33kb. Then the binary data is 33630 and its Base64 string length is 44840 after conversion.

So when i include it in the json and it will become a token, the url size now is 60k plus. So it resulted to 'HTTP Error 414. The request URL is too long'

are there any ways to include the parameters in the body instead of adding it in the url?

leonseremelis-tass commented 7 months ago

Yes, usually we recommend sending data in the request body instead of including it in the URL. In the context of an HTTP request, you can use the HTTP POST method to include parameters in the request body.

Here's a general guide on how you can modify your request:

  1. Change the HTTP Method:

    • Instead of using a GET request where parameters are included in the URL, switch to a POST request. This is done by changing the method in your request to POST.
  2. Include Data in the Request Body:

    • Move the data you were including in the URL (e.g., the JSON representation of the PO Invoice) into the body of the HTTP request. This way, the data won't contribute to the length of the URL.

By using a POST request and including data in the request body, you should be able to avoid URL length limitations..

geejavier0422 commented 6 months ago

Sorry for the late reply. I tried using form-data, and x-www-form-urlencoded as the body :

var formData = new Dictionary<string, string>
                {
                    { "method", apiMethod },
                    { "appcode", _tassConfiguration.AppCode },
                    { "company", _tassConfiguration.CompanyCode },
                    { "v", _tassConfiguration.Version.ToString() },
                    { "token", HttpUtility.UrlEncode(encrypted) }
                };

                // Convert form data to HttpContent
                var content = new FormUrlEncodedContent(formData);

However, I keep getting "unauthorized: Access denied due to invalid credentials." I used these credentials when I use the URL with parameters, and it works well aside from including documents that caused the long URL.

leonseremelis-tass commented 6 months ago

You're on the right track, and it's likely unnecessary to re-encode the token using HttpUtility.UrlEncode() if you're already encoding the form as x-www-form-urlencoded. If you are still facing challenges, please don't hesitate to reach out to our support team. They'll be more than happy to provide you with further assistance.