Solved

Configure IIS to process JSON

Posted on 2016-09-15
10
74 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
 

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
3 Use Cases for Connected Systems

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

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

What is an ISAPI filter?   •      It's an assembly (.dll file) that can add or change the way IIS works.   •      They can be enabled globally for your web server or on a site-by-site basis.   When the IIS server receives a request, enabling the ISAPI fi…
We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now