Solved

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

Posted on 2016-09-15
8
281 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 51

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 51

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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 51

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP to HTTPS but have 2 sub sites 8 35
curl to HTTP request VBA 4 71
Codeigniter Query Help Returning JSON 4 46
Save json data from URL using SSIS 1 22
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
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…

713 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