Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Configure IIS to process JSON

Posted on 2016-09-15
10
Medium Priority
?
200 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 41

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 84

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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 84

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 41

Accepted Solution

by:
Kyle Abrahams earned 1000 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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