Unity returns “400 (Bad Request) Error” on sending login-mutation to local graphql-server

Unity returns “400 (Bad Request) Error” on sending login-mutation to local graphql-server


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:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibtypesjson.js:160:10)<br> &nbsp; &nbsp;at parse (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibtypesjson.js:83:15)<br> &nbsp; &nbsp;at C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesbody-parserlibread.js:128:18<br> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:203:9)<br> &nbsp; &nbsp;at invokeCallback (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.js:231:16)<br> &nbsp; &nbsp;at done (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.js:220:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:snapshotagent_work43ssrcPLFExeServicesGraphQLNodeJsServernode_modulesraw-bodyindex.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.

Share
Improve this question

New contributor

lnmr24 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

  • You say it works from postman. That mutation string doesnt look right

    – BugFinder

    1 min ago


Load 6 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *