Solved

Configure IIS to process JSON

Posted on 2016-09-15
10
113 Views
Last Modified: 2016-09-16
I'm trying process JSON data being sent to my web service from an HTTP POST.

The data is coming in as RAW data in the body of the post but my server is reporting that data at the root level is invalid.

I know the json data is correctly formatted so I'm sure my IIS is not processing it properly.

How do I configure my IIS instance to correctly parse the json?
0
Comment
Question by:BrianFord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41800677
From:
http://www.uipress.com/add-json-handler-support-in-iis-7/


Open IIS Manager
Display properties for the IIS Server
Click MIME Types and then add the JSON extension:
File name extension: .json
MIME type: application/json
Go back to the properties for IIS Server
Click on Handler Mappings
Add a script map
Request path: *.json
Executable: C:\WINDOWS\system32\inetsrv\asp.dll
Name: JSON

Also if you're posting as json make sure you set the content type as the default I believe is XML.

EG:

request.OnBeforeDeserialization = resp => 
{                resp.ContentType = "application/json"; }; 

Open in new window

0
 

Author Comment

by:BrianFord
ID: 41800685
thanks Kyle,

I've added those but still getting the error. I'm not the one POSTing the request, it's coming from a 3rd party app and they are posting correctly.

It has to be something on my IIS server that's not handling it correclty
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41800712
What is the error message?  I know of programs written in PHP and ASP.NET for processing JSON data but I never heard of the web server 'processing' it other than setting the correct 'Content Type' when it is served.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:BrianFord
ID: 41800721
Perhaps my terminology is bit off :)

The POST is being received by an ASP.NET Web Service running on my IIS server

The error is "Data at the root level is invalid. Line 1, position 1"

I know that the data being sent is in valid JSON format and the receiving method is at this time not doing any work, it basically just the method declaration that returns a 200 response like this:

 
<HttpPost()>
    Public Function TestJSON(<FromBody> ByVal pJSON As String) As Integer
        Return 200
    End Function

Open in new window


The problem is that IIS is returning a 500 error, it's like it cant 'handle' the data in the POST, sorry if I'm not explaining this very well :)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41800746
500 errors are most often caused by a program running on the server rather than the server itself.  Do you have a simple test program that you can use to send a simple JSON string to the server?
0
 

Author Comment

by:BrianFord
ID: 41801287
I just tested with https://www.getpostman.com/ using the JSON string:

{
  "status": "error",
  "message": "contact does not exist",
  "correlationId": "7d60e72c-98f4-4ca4-916e-89a792d7266e",
  "requestId": "30770fba-1ded-4d9f-8d2e-0277564729e1"
}

And got the following 500 error:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
   at System.Xml.XmlReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
   --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 41801757
are you using a normal web service?

Did you add:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

Open in new window



Also from: http://forums.asp.net/p/1054378/2338982.aspx#2338982

add the following to the webserver of your web.config:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>

Open in new window

0
 

Author Comment

by:BrianFord
ID: 41801898
It'a standard web service (VB not C#)

I'm actually considering changing this app to WebAPI 2 based service, I think in the long run it will be effecient
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 41801911
I'm with Kyle on this.  Your error message says that your service is expecting SOAP/XML and not JSON.  The two methods and protocols are not interchangeable.
0
 

Author Closing Comment

by:BrianFord
ID: 41802149
Thanks for trying guys,

I have rebuilt the app using WebAPI & controllers etc. and everything is now working perfectly
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
What You Need to Know when Searching for a Webhost Provider
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question