Solved

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

Posted on 2016-09-15
8
69 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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
>>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
 

Author Comment

by:BrianFord
Comment Utility
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
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.

 

Author Comment

by:BrianFord
Comment Utility
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 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for trying Ryan,

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

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

12 Experts available now in Live!

Get 1:1 Help Now