microsoft / PowerPlatformConnectors

This is a repository for Microsoft Power Automate, Power Apps, and Azure Logic Apps connectors
https://aka.ms/connectors
MIT License
964 stars 1.25k forks source link

[BUG] Http request failed: the content was not a valid JSON #2984

Open jeroenwolsink opened 11 months ago

jeroenwolsink commented 11 months ago

Type of Connector

Independent Publisher Connector

Name of Connector

VIES (Independent Publisher)

Describe the bug

In both a Power Automate cloud flow as a Power App, the connector fails with the following error details:

Http request failed: the content was not a valid JSON. Error while parsing JSON: 'Unexpected character encountered while parsing value: O. Path '', line 0, position 0.'

Is this a security bug?

No, this is not a security bug

What is the severity of this bug?

Severity 1 - Connector is broken or there's a very serious issue

To Reproduce

Expected behavior

A JSON body with the result of the VAT validation

Environment summary

Power Page (canvas app) within Dynamics 365 account form Power Automate cloud flow for testing

Additional context

Sahu-Debasish commented 11 months ago

The error message "Http request failed: the content was not a valid JSON" typically indicates that the response received from the VIES (Value Added Tax Information Exchange System) service is not in the expected JSON format. To resolve this issue, you can take the following steps:

  1. Check the API Endpoint: Ensure that you are using the correct API endpoint for the VIES service. The URL or endpoint might have changed, so verify that your connector configuration is up-to-date.

  2. Check Request Headers: Make sure that your HTTP request includes the appropriate headers, such as the "Content-Type" header specifying "application/json" as the content type. The absence of headers or incorrect headers can lead to parsing issues.

  3. Validate Response: The error suggests that the response from the VIES service is not valid JSON. You should inspect the actual response content to understand what is being returned. You can use tools like Postman or a simple HTTP client to make a direct request to the VIES service and examine the response. Ensure that it is returning valid JSON.

  4. Handle Non-JSON Responses: If the VIES service occasionally returns non-JSON responses (e.g., an error message in HTML format), you may need to implement error handling in your connector to account for these cases. This could involve checking the response content type and taking appropriate action when it's not JSON.

  5. Connector Configuration: Review your connector configuration for any issues, such as incorrect authentication or input parameters. Ensure that the country code and VAT number are being passed correctly.

  6. Retry Mechanism: Consider implementing a retry mechanism in your connector or flow to handle transient errors. Sometimes, services like VIES might experience temporary issues that could cause occasional failures.

  7. Logging and Monitoring: Implement logging and monitoring in your connector or flow to track the responses received from the VIES service. This can help you diagnose issues and gather data for troubleshooting.

  8. Documentation and Support: Consult the official documentation for the VIES service or seek support from the service provider or community forums. They may provide insights into any recent changes or known issues with the service.

  9. Alternative Approaches: If you continue to face difficulties with the VIES service, consider exploring alternative methods for VAT validation or using different validation services that may better suit your needs.

By following these steps and ensuring that your connector configuration is correct and handles various response scenarios, you should be able to resolve the issue of receiving non-valid JSON responses from the VIES service.

jeroenwolsink commented 11 months ago

Hi @DebasishBlockchain

Thank you for your reply. Unfortunately we can't change the way the connector creates a request to the VIES service. See the code from the connector. It uses a soap request to get the information.

I created a request in postman just like the connector is doing.

request-1

When a add application/xml as content-type header the VIES service respond with a 500 eror see screenshot below.

request-2

@tposzytek Can this be the issue with the connector?

Sahu-Debasish commented 11 months ago

It appears that you have a C# script that makes a SOAP request to the VIES (VAT Information Exchange System) service and then converts the SOAP response into JSON format. The script takes input parameters for countryCode and vatNumber, sends a SOAP request to the VIES service, and then processes the response.

Certainly, I'll provide a solution to address the issue of receiving a 500 internal server error when using the application/xml content-type header with the VIES service.

The problem in your code appears to be in the way you're sending the SOAP request to the VIES service. You're constructing the SOAP envelope, but you are not setting the appropriate content type or sending the XML data in the request body correctly. Here's a corrected version of your code:

using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;

public class Script : ScriptBase
{
    public override async Task<HttpResponseMessage> ExecuteAsync()
    {
        return await HandleForwardOperation().ConfigureAwait(false);
    }

    private async Task<HttpResponseMessage> HandleForwardOperation()
    {
        HttpResponseMessage response;
        var requestBody = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
        var data = JObject.Parse(requestBody);
        var countryCode = ((string)data["countryCode"]).Trim();
        var vatNumber = ((string)data["vatNumber"]).Trim().Replace(" ", string.Empty).Replace("-", string.Empty).Replace(".", string.Empty);

        var url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";

        try
        {
            using (var httpClient = new HttpClient())
            {
                var soapEnvelope = $@"
                    <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:web='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
                        <soapenv:Header/>
                        <soapenv:Body>
                            <web:checkVat>
                                <web:countryCode>{countryCode}</web:countryCode>
                                <web:vatNumber>{vatNumber}</web:vatNumber>
                            </web:checkVat>
                        </soapenv:Body>
                    </soapenv:Envelope>";

                var content = new StringContent(soapEnvelope, System.Text.Encoding.UTF8, "application/soap+xml");

                var httpResponse = await httpClient.PostAsync(url, content).ConfigureAwait(false);

                if (httpResponse.IsSuccessStatusCode)
                {
                    var responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                    response = new HttpResponseMessage(HttpResponseMessageStatusCode.OK);
                    response.Content = CreateJsonContent(responseContent);
                    return response;
                }
                else
                {
                    // Handle the error here, log or return a specific response based on the error status.
                    response = new HttpResponseMessage(httpResponse.StatusCode);
                    response.Content = CreateJsonContent(httpResponse.ReasonPhrase);
                    return response;
                }
            }
        }
        catch (Exception e)
        {
            response = new HttpResponseMessage(HttpResponseMessageStatusCode.BadRequest);
            response.Content = CreateJsonContent(e.Message);
            return response;
        }
    }
}

Changes made in the corrected code:

  1. The SOAP envelope is constructed correctly with the proper namespaces.
  2. The HttpClient class is used to make the HTTP POST request with the correct content type (application/soap+xml).
  3. Proper error handling is added to handle responses from the VIES service, including returning a specific response for error cases.

Please note that this code assumes that the VIES service endpoint and SOAP envelope structure are correct. If there are any specific requirements or changes in the VIES service API, you should refer to their documentation to ensure compatibility.

If you can provide more specific error messages or details about the response you receive from the VIES service, I can offer more targeted advice.

troystaylor commented 11 months ago

@DebasishBlockchain I would like to understand why you commented on this bug. It doesn't appear that you are a contributor to this project and your fork doesn't include any additional code. Both of the comments you have left aren't helpful, in fact, they both look you've just plugged the bug into ChatGPT without any further testing. The custom code provided above doesn't work, as a new HttpClient isn't allowed. @jeroenwolsink I have confirmed that the certified connector is returning the error you have reported. This can be temporarily overcome by creating your own connector using the Swagger file (https://raw.githubusercontent.com/microsoft/PowerPlatformConnectors/dev/independent-publisher-connectors/VIES%20by%20European%20Commission/apiDefinition.swagger.json) and then enabling the custom code provided by Tomasz (https://raw.githubusercontent.com/microsoft/PowerPlatformConnectors/dev/independent-publisher-connectors/VIES%20by%20European%20Commission/script.csx) but remove the first line (#r "Newtonsoft.Json"). @vmanoharas Please fast-track the update PR: https://github.com/microsoft/PowerPlatformConnectors/pull/3006

tposzytek commented 11 months ago

Hey, I am aware of the issue and will try to work on it asap, but I am afraid this will happen not sooner than next week. I am really sorry for inconveniences.

tposzytek commented 11 months ago

@troystaylor I see you have already implemented the fix for the bug and it's waiting for deployment? Thank you a tone! :)

troystaylor commented 11 months ago

@troystaylor I see you have already implemented the fix for the bug and it's waiting for deployment? Thank you a tone! :)

I got your back, Tomasz. I would have checked it sooner, but I was at MPPC last week.

tposzytek commented 11 months ago

You were too? How is it possible we haven't met 😔 anyways thanks a ton!

Best regards,

Tomasz Poszytek

e-mail: @.*** tel: +48 603 993 372 www/ blog: https://poszytek.euhttps://poszytek.eu/ twitter: @TomaszPoszytek https://aka.ms/AAb9ysg


From: Troy Taylor @.> Sent: Monday, October 9, 2023 3:59:41 PM To: microsoft/PowerPlatformConnectors @.> Cc: Tomasz Poszytek @.>; Mention @.> Subject: Re: [microsoft/PowerPlatformConnectors] [BUG] Http request failed: the content was not a valid JSON (Issue #2984)

@troystaylorhttps://github.com/troystaylor I see you have already implemented the fix for the bug and it's waiting for deployment? Thank you a tone! :)

I got your back, Tomasz. I would have checked it sooner, but I was at MPPC last week.

— Reply to this email directly, view it on GitHubhttps://github.com/microsoft/PowerPlatformConnectors/issues/2984#issuecomment-1753070861, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMCPVG2MZBKSERTAU42JWADX6P7M3AVCNFSM6AAAAAA5SIONMKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJTGA3TAOBWGE. You are receiving this because you were mentioned.Message ID: @.***>