user11060150
user11060150

Reputation: 31

Index out of range Exception when trying to get a webhook request, how can I solve this

I am trying to implement a Webhook using asp.net core which gets the parameters and use them as part of a Fulfilment text.

The Webhook request however is returning a index out of range exception

[Route("Values")]    
public class ValuesController : Controller
{
    private static readonly JsonParser jsonParser = new JsonParser (JsonParser.Settings.Default.WithIgnoreUnknownFields(true));

    [HttpPost]
    public ContentResult DialogAction()
    {
        WebhookRequest request;

        using (var reader = new StreamReader(Request.Body))
        {
            request = jsonParser.Parse<WebhookRequest>(reader.ReadToEnd());
        }

        double number = 0;
        double number1 = 0;
        double number2 = 0;

        if (request.QueryResult.Action == "entry_data")
        {
            var paramzee = request.QueryResult.Parameters;
            number = paramzee.Fields["number"].NumberValue;
            number1 = paramzee.Fields["number1"].NumberValue;
            number2 = paramzee.Fields["number2"].NumberValue;
        }

        WebhookResponse response = new WebhookResponse
        {

            FulfillmentText = $"Thank You, you have successfully completed your registration. " +
            $"You have entered First Name: {number1}" +
            $"Last Name : {number} and Phone Number: {number2}"
        };

        string responseJson = response.ToString();
        return Content(responseJson, "application/json");
    }
  }
}

Here is the webhook request

curl -X POST -H ': ' -H 'Content-Type: application/json' -d {"responseId":"0c16b026-acb8-4102-89d3-3436ffc0b652-ee1dc704","queryResult":{"queryText":"Registration Form First Name:12345 Last Name: Phone Number:","action":"entry_data","parameters":{"number":12345},"allRequiredParamsPresent":true,"fulfillmentMessages":[{"text":{"text":[""]}}],"intent":{"name":"projects/alarm-rshrnc/agent/intents/aadc26e2-5d53-4341-8d68-06b937491e2c","displayName":"ProcessData"},"intentDetectionConfidence":0.6842971,"languageCode":"en"},"originalDetectIntentRequest":{"payload":{}},"session":"projects/alarm-rshrnc/agent/sessions/df10d4c0-67c3-3527-0395-ec5c489a1345"} https://webhook.site/4f51520c-d67d-4db2-b742-10f31a965896

the webhook is supposed to return a fulfilment text with some of the parameters included in the response however i get an index out of range exception.

Upvotes: 2

Views: 159

Answers (1)

Prisoner
Prisoner

Reputation: 50721

If you're doing this curl command on UNIX, it seems likely that it is not sending that entire JSON string as the body. Since there are spaces in it, it is likely your shell is breaking the parameters on the space, so only sending part of the string as the data.

Quote your entire JSON string using single quotes '

Upvotes: 0

Related Questions