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> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError (C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\body-parser\lib\types\json.js:160:10)<br> &nbsp; &nbsp;at parse (C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\body-parser\lib\types\json.js:83:15)<br> &nbsp; &nbsp;at C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\body-parser\lib\read.js:128:18<br> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:203:9)<br> &nbsp; &nbsp;at invokeCallback (C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\raw-body\index.js:231:16)<br> &nbsp; &nbsp;at done (C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\raw-body\index.js:220:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\snapshot\agent\_work\43\s\src\PLF\Exe\Services\GraphQL\NodeJsServer\node_modules\raw-body\index.js:280:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:events:525:35)<br> &nbsp; &nbsp;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.

lnmr24
  • 1
  • You say it works from postman. That mutation string doesnt look right – BugFinder Aug 21 '23 at 11:50
  • The default HTTP header are different in Postman and c#, You may need to add missing headers like UserAgent to get code to work. – jdweng Aug 21 '23 at 12:06
  • @jdweng where can I find the default headers that postman uses or that the graphql server needs? or is UserAgent the only header missing from the query? edit: i fhave found the header :D i try if it works now – lnmr24 Aug 21 '23 at 14:08
  • @BugFinder mhh but i create it in postman and copy it into the string mutation – lnmr24 Aug 21 '23 at 14:09
  • Postman has a visualize tab : https://stackoverflow.com/questions/33793629/postman-how-to-see-request-with-headers-and-body-data-with-variables-substitut – jdweng Aug 21 '23 at 15:38

0 Answers0