Solved

Configure IIS to process JSON

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

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 82

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 82

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 39

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 82

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Debug Tools to analyse IIS process: This article focus on taking memory dumps from IIS to determine which code is taking more time and to analyse which calls hangs/causes more CPU usage. To take dumps,download the following. Install1: To st…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

18 Experts available now in Live!

Get 1:1 Help Now