Link to home
Start Free TrialLog in
Avatar of David_zu
David_zu

asked on

Extract Original JSON Request in ASP.NET

I am using ASP.NET 4.7 (not ASP.NET Core) to design a web page, which receive Webhook request from Dialogflow. I use following code to extract the JSON data from request.

using (var reader = new StreamReader(Request.InputStream))
{
    string reqString = reader.ReadToEnd();
}

Open in new window


However, I found the double quotes in the original request are missing in the content. Original message is

{"responseId":"0b0b6903-60be-4fdf-919b-f20103bfd1ec-21947381","queryResult":{"queryText":"I need to book a hotel room with queen size bed.","parameters":{"size":"queen"},"allRequiredParamsPresent":true,"fulfillmentMessages":[{"text":{"text":[""]}}],"intent":{"name":"projects/aets-mknkjs/agent/intents/1727505d-2499-42c4-95ec-7385b5da17a3","displayName":"hotelbooking"},"intentDetectionConfidence":0.7410408,"languageCode":"en"},"originalDetectIntentRequest":{"payload":{}},"session":"projects/aets-mknkjs/agent/sessions/d55e1ae8-c5d3-1ecf-7d6a-9f27391b6b6c"}

Open in new window


But I extracted from Request.InputStream does not have double quotes.

{responseId:0b0b6903-60be-4fdf-919b-f20103bfd1ec-21947381,
queryResult:{queryText:I need to book a hotel room with queen size bed.,
parameters:{size:queen},
allRequiredParamsPresent:true,
fulfillmentMessages:[{text:{text:[]}}],intent:{name:projects/aets-mknkjs/agent/intents/1727505d-2499-42c4-95ec-7385b5da17a3,displayName:hotelbooking},intentDetectionConfidence:0.7410408,languageCode:en},originalDetectIntentRequest:{payload:{}},session:projects/aets-mknkjs/agent/sessions/d55e1ae8-c5d3-1ecf-7d6a-9f27391b6b6c}

Open in new window


How can I retain or extract the orginal message content?
Avatar of Douglas Suyemoto
Douglas Suyemoto
Flag of United States of America image

How are you extracting the data?  Is there some other code that's missing?  Are you just debugging and copying and pasting the value of 'reqString'?
Avatar of David_zu
David_zu

ASKER

I use reference from https://meteatamel.wordpress.com/2018/09/24/dialogflow-fulfillment-with-c-and-app-engine/

But the example is Asp.Net Core. I changed it to use in Asp.Net 4.7.

the basic idea is to use JsonParser class to create the WebhookRequest instance, so I can easily get information and build up WebhookResponse.

https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/json-parser

https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/api/Google.Cloud.Dialogflow.V2.WebhookRequest.html
I use Wireshark to capture the incoming request. the data are correctly inside the double quotes. But when I read it from Asp.Net, the quotes are missing so JasonParser throws the invalid format exception.
Well if you're receiving the correct data into the web service then he has to be the way you're extracting it.  Can you post the code for the JsonParser? There doesn't seem to be anything wrong with the above code you already posted to read the stream.
Sorry for the late reply. I burnt my weekend for an important project. The JSON I captured in WireShark is:

{"responseId":"0b0b6903-60be-4fdf-919b-f20103bfd1ec-21947381","queryResult":{"queryText":"I need to book a hotel room with queen size bed.","parameters":{"size":"queen"},"allRequiredParamsPresent":true,"fulfillmentMessages":[{"text":{"text":[""]}}],"intent":{"name":"projects/aets-mknkjs/agent/intents/1727505d-2499-42c4-95ec-7385b5da17a3","displayName":"hotelbooking"},"intentDetectionConfidence":0.7410408,"languageCode":"en"},"originalDetectIntentRequest":{"payload":{}},"session":"projects/aets-mknkjs/agent/sessions/d55e1ae8-c5d3-1ecf-7d6a-9f27391b6b6c"}

Open in new window


This JSON is valid. I validated it in https://jsonlint.com/.

But when I use my code to get the stream, the double quotes are missing. I try to validate it again, and it is invalid.

{responseId:0b0b6903-60be-4fdf-919b-f20103bfd1ec-21947381,
queryResult:{queryText:I need to book a hotel room with queen size bed.,
parameters:{size:queen},
allRequiredParamsPresent:true,
fulfillmentMessages:[{text:{text:[]}}],intent:{name:projects/aets-mknkjs/agent/intents/1727505d-2499-42c4-95ec-7385b5da17a3,displayName:hotelbooking},intentDetectionConfidence:0.7410408,languageCode:en},originalDetectIntentRequest:{payload:{}},session:projects/aets-mknkjs/agent/sessions/d55e1ae8-c5d3-1ecf-7d6a-9f27391b6b6c}

Open in new window


JsonParser class is defined by Google Lib. It is working fine with JSON with double quote (original message).
https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/json-parser

I suspect when I get the content string from Request (HttpRequest Class), the output has been serialized. But I am not sure which part I shall intercept.
If you are using the code above, if you send the following JSON string:

{
	"Test" : "Value"
}

Open in new window

you should get back this from the streamreader:

"{\n\t\"Test\" : \"Value\"\n}"

I assume there is code you have not presented here to further process the "reqStream" variable.  Otherwise, I don't have much else to go on as the code you posted so far does not seem to be an issue.  If you are using a web service, post the entire controller method code.  If you're using an ApiController class, you can also parse the JSON directly to a class by adding to the controller method.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.