0
i try to login into my local graphql-server from unity. the mutation works from apollo-client and postman, but with the unity.webrequest i get 2 errors.
the first one:
Network error: HTTP/1.1 400 Bad Request
UnityEngine.Debug:LogError (object)
GraphApi/<LoginMutation>d__3:MoveNext () (at Assets/GraphApi.cs:61)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
and the second:
Response body: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token % in JSON at position 0<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibtypesjson.js:160:10)<br> at parse (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibtypesjson.js:83:15)<br> at C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibread.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:203:9)<br> at invokeCallback (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.js:231:16)<br> at done (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.js:220:7)<br> at IncomingMessage.onEnd (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.js:280:7)<br> at IncomingMessage.emit (node:events:525:35)<br> at endReadableNT (node:internal/streams/readable:1358:12)</pre>
</body>
</html>
UnityEngine.Debug:LogError (object)
GraphApi/<LoginMutation>d__3:MoveNext () (at Assets/GraphApi.cs:64)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)here
and here is the c# code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
using System.Net.Http;
using System;
using System.Threading.Tasks;
using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics;
using static UnityEditor.Progress;
using static UnityEditor.ShaderData;
using System.Drawing;
using Unity.VisualScripting;
public class GraphApi : MonoBehaviour
{
public TextMeshProUGUI text;
public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
// Bypass-Zertifikatvalidierung
return true;
}
}
void Start()
{
StartCoroutine(LoginMutation());
}
IEnumerator LoginMutation()
{
Debug.Log(@"""" == """);
string graphqlEndpoint = "https://notebook65/graphql/";
string mutation = @"mutation Login {
login(username: ""admin"", password: ""xxx"") {
token
error {
code
description
}
}
} ";
string jsonRequestBody = "{"mutation" : "" + mutation + ""}";
Debug.Log(jsonRequestBody);
text.text = jsonRequestBody;
using UnityWebRequest www = UnityWebRequest.Post(graphqlEndpoint, jsonRequestBody);
//www.SetRequestHeader("Accept", "application/json");
www.SetRequestHeader("Content-Type", "application/json");
// Deaktivieren der SSL-Verifikation
www.certificateHandler = new BypassCertificate();
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Network error: " + www.error);
if (!string.IsNullOrEmpty(www.downloadHandler.text))
{
Debug.LogError("Response body: " + www.downloadHandler.text);
}
}
else
{
string responseBody = www.downloadHandler.text;
Debug.Log("Response body: " + responseBody);
text.text = responseBody;
}
}
}
I have tried a lot of different versions of the query s, whether with "query" or "mutation", upper or lower case, but always get the same error.
New contributor
1
You say it works from postman. That mutation string doesnt look right
1 min ago
|