I am doing a web request to Post to a site. It gets posted fine to the site. But on the return I am getting this error on the json.
I used json to csharp to push the json into the classes…which has worked fine for all the other json I have been doing.
JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Newtonsoft.Json.JsonTextReader.ParseValue () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonTextReader.Read () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonReader.ReadAndMoveToContent () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Web_request+<PostJob>d__32.MoveNext () (at Assets/CODE/Web_request.cs:220)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <a979f18d51af41179d12b797e8c5be14>:0)
This is the web request:
IEnumerator PostJob(string PostURL)
{
string JsonData_ = "{\"input\":{\"urn\":" + string + "},\"output\":{\"formats\":[{\"type\":\"arkit\",\"scene\":" + string + "}]}}";
UnityWebRequest postJob = UnityWebRequest.Post(PostURL, JsonData_);
postJob.SetRequestHeader("Content-Type", "application/json");
postJob.SetRequestHeader("Authorization", "Bearer " + accessToken.text.ToString());
yield return postJob.SendWebRequest();
if (!postJob.isNetworkError || !postJob.isHttpError)
{
var ReturnedJson = JsonConvert.DeserializeObject<Posted_Return>(postJob.downloadHandler.text);
Debug.Log(ReturnedJson);
PostedJobResult.text = ReturnedJson.result;
Debug.Log("Payload Posted to Server.");
}
else
{
Debug.Log(postJob.error);
}
}
The JsonConvert line is throwing the error.
I don’t know how to fix this error.
Print this out:
postJob.downloadHandler.text
See if it’s valid JSON.
I’m getting this is I print that out.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
In Postman it returns this:
{
"result": "created",
"urn": "urn_value",
"acceptedJobs": {
"output": {
"formats": [
{
"type": "arkit",
"scene": "string"
}
]
}
}
}
And the web response sends the data and works…it is just this return.
Clearly there is something wrong with your request. The server is giving you back an error.
Even if the request is working?
If the server is responding with this:
All_American:
Error
Bad Request
I wouldn’t categorize that as “working”.
It does what it is supposed to on the server. I know obviously something is wrong.
Notice it says “bad request” there’s something wrong with the way you’re creating and sending the request in Unity. it’s unfortunate the server isn’t providing any more details than that. Try printing this variable after you build it: JsonData_ and make sure it’s valid JSON (and looks like what you’re sending from postman)
Also shouldn’tif (!postJob.isNetworkError || !postJob.isHttpError)beif (!postJob.isNetworkError && !postJob.isHttpError)?
Already did. The JsonData_ value looks as though it should when sending it.
When I do that it just goes right to the “Bad Request”. I’m pulling my hair out…
Well yeah - that’s what you want. The server is returning an error , you want to treat the error like an error. Obviously that doesn’t fix your immediate problem, but you don’t want errors to be treated like successes in the future - or you’ll just get confusing errors like this JSON parse error (because it’s trying to read HTML instead of JSON).
Anyways - there’s some difference between what you’re sending to the server in Unity and what you’re sending in Postman. Suss out what that difference is and you’ll get there. It’s either a difference in the URL, the headers, or the actual JSON payload. look at each one of those and make sure they match exactly.
PraetorBlue:
Anyways - there’s some difference between what you’re sending to the server in Unity and what you’re sending in Postman. Suss out what that difference is and you’ll get there. It’s either a difference in the URL, the headers, or the actual JSON payload. look at each one of those and make sure they match exactly.
I did all that. Can’t figure it out.
If you’re willing to share all of the relevant data then maybe forum-denizens can help see what you’re missing:
the request url in postman
the headers in postman
the json payload in postman
the HTTP Method (GET, POST, PUT?) in postman
the request url in Unity
the headers in Unity
the json payload (JsonData_) in Unity
the HTTP Method (GET, POST, PUT?) in Unity
Something must be different.
Also try using a JSON linter like https://jsonlint.com/ to double check that your request JSON is valid json.
All the URL is is to an Autodesk server. That’s accurate. Then I am using post in Unity and tested it in Postman with post. Postman returns what I showed you and I am sending what is in the above code. all headers are the same. Nothing special anyway.
I triple checked if what I am sending is valid. Besides me injecting strings.
All_American:
All the URL is is to an Autodesk server. That’s accurate. Then I am using post in Unity and tested it in Postman with post. Postman returns what I showed you and I am sending what is in the above code. all headers are the same. Nothing special anyway.
I triple checked if what I am sending is valid. Besides me injecting strings.
It could be something as simple as the data not being valid. For example in here:
string JsonData_ = "{\"input\":{\"urn\":" + string + "},\"output\":{\"formats\":[{\"type\":\"arkit\",\"scene\":" + string + "}]}}";
It could be that your “urn” value or your “scene” value are not valid/known to the server.
Actually I suspect strongly your error is that you are missing quotes around your “injected” string values here:
string JsonData_ = "{\"input\":{\"urn\":" + string + "},\"output\":{\"formats\":[{\"type\":\"arkit\",\"scene\":" + string + "}]}}";
Before the JsonData_ is sent I have checked it and it looks like it is supposed to. The values are what the server needs to do the job and the job does get done.
Its info on what model to send for a conversion operation. And it does complete successfully.
I think it is what is being returned that’s jacked. But I don’t know.
PraetorBlue:
Actually I suspect strongly your error is that you are missing quotes around your “injected” string values here:
string JsonData_ = "{\"input\":{\"urn\":" + string + "},\"output\":{\"formats\":[{\"type\":\"arkit\",\"scene\":" + string + "}]}}";
I was thinking that to…but it looks good and sends and works…lolol
You have quotes around your injected string values? From your code it doesn’t look like you’re including them.
If you don’t have quotes around the strings, it’s not valid JSON. And that would be why the server is giving you an error back.