Solved

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

Posted on 2016-09-15
8
137 Views
Last Modified: 2016-09-16
I hope I can explain this fully :)

I have a web service URL that accepts a JSON string from a 3rd party application but it is failing with the above error.

Two things to note:
1. if I configure the 3rd party app to send to a public testing URL (http://requestb.in) it seems to work just fine
2. If I copy the raw body and test it directly on my server, it also works perfectly and return a 200 response

However, when I configure the 3rd party app to send to my URL my service returns 500 response. I have added the application/json mime type to IIS.

I'm sure the issue is on my end but I'm at a loss as to how to troubleshoot this further

The attached image shows the test screen and the resulting 200 response
testing.png
Any help would be greatly appreciated
0
Comment
Question by:BrianFord
  • 5
  • 3
8 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41799694
at your web service, did it provide a sample on how the request parameters should look like?

also, what's your 3rd party app? is it in .net, java, php, etc?
0
 

Author Comment

by:BrianFord
ID: 41799718
The 3rd party app is HubSpot (hubspot.com) I have no control over what/how they send the data but I'm certain their POST is correct, it's something my end.

It's just a string parameter, however I'm not sure I should be using a parameter as the data is sent in the RAW BODY?
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41799825
>>Data at the root level is invalid. Line 1, position 1
I'm actually facing similar issue recently but I was managed to fix the issue by applying similar fixes in this link:

Data at the root level is invalid. Line 1, position 1.
http://www.ipreferjim.com/2014/09/data-at-the-root-level-is-invalid-line-1-position-1/

you can also get some explanation from this article:

Data at the root level is invalid
http://madskristensen.net/post/data-at-the-root-level-is-invalid

both required fixes in coding.

for your case, perhaps you could advise the support from HubSpot (hubspot.com) and see if they can provide you some solutions to solve your issue.

I'm thinking the issue relying on the approach we send the request to your web service.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:BrianFord
ID: 41799869
I think what I need to do here is get the RAW BODY from the request and not a parameter, I've seen several posts using the <FromBody> method but my code doesn't recognize teh keyword for some reason
0
 

Author Comment

by:BrianFord
ID: 41800404
I've looked at both of those and they don't really answer my question as they are both talking about XML documents and my issue is with a JSON format string.

my web service is just about as simple as it gets, it does no processing and simply return a 200.

But when I POST the JSON string to it I get the Data at the root level is invalid error

This is the web method (HttpPost)


Imports System.Web.Services
Imports System.Web.Http

<System.Web.Script.Services.ScriptService()>
<WebService(Namespace:="http://XXXXXXXXX.com/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class HubSpot
    Inherits System.Web.Services.WebService

    <HttpPost()>
    Public Function ConvertMarketingQualifiedToLead(<FromBody> ByVal pJSON As String) As Integer

        Return 200

    End Function

End Class

Open in new window


Can't get much simpler than that :(
0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41800848
can this web service be accessible publicly?

on a separate note, are you able to use test tool such as "Postman"?

https://www.getpostman.com/

you can try to test your web service there, and if the posting is succeeded, can you show me what is inside the dialog after clicking the "Generate Code"?
SnapShot.png
and see if you could get a status 200 as the response status?
0
 

Author Comment

by:BrianFord
ID: 41801278
Ryan,

I tested using postman and still got a 500 error :(

Here's what I used: unfortunately I can'd display the host info :)

POST /HubSpot.asmx HTTP/1.1
Host: xxxxxxx.com
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 190b3c0e-7603-cb90-d567-961cb0b98fb9

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

And here is the result:
<?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
 

Author Closing Comment

by:BrianFord
ID: 41802150
Thanks for trying Ryan,

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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

805 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