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
Solved

Configure IIS to process JSON

Posted on 2016-09-15
10
87 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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