1

need help in formatting a graphql mutation in java code. In postman it looks like this->.postman and what I have tried is-> my code and the error I am getting is--> error I am getting. Thanks in advance :-)

Raghu K
  • 57
  • 1
  • 10
  • Help me to understand what languages are being used. It was mentioned Java, but the error that is being provided is returning a reference to a JS file. – Manny Aug 09 '21 at 23:50
  • Postman provides a code snippet that exports in various formats, hopefully one of the examples shows how to escape for your use-case...providing now... – Manny Aug 09 '21 at 23:55

1 Answers1

1

Escaping will be language dependent and so I have provided a few examples below based on the sample you provided.

Postman provides a code snippet that exports in various formats

cURL

curl --location --request GET '' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation{\n    Create(\n        request: \"Xboxx\",\n        GoalAmount: 123,\n        GoalTargetData: \"15/09/2021\",\n        ImagerRef:\"etst ref\"\n    ){\n        Id\n        Name\n        GoalAmount\n        GoalTargetDate\n        ImageRef\n    }\n}","variables":{}}'

java

Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.get("")
  .header("Content-Type", "application/json")
  .body("{\"query\":\"mutation{\\n    Create(\\n        request: \\\"Xboxx\\\",\\n        GoalAmount: 123,\\n        GoalTargetData: \\\"15/09/2021\\\",\\n        ImagerRef:\\\"etst ref\\\"\\n    ){\\n        Id\\n        Name\\n        GoalAmount\\n        GoalTargetDate\\n        ImageRef\\n    }\\n}\",\"variables\":{}}")
  .asString();

javascript

//javascript - fetch
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var graphql = JSON.stringify({
  query: "mutation{\n    Create(\n        request: \"Xboxx\",\n        GoalAmount: 123,\n        GoalTargetData: \"15/09/2021\",\n        ImagerRef:\"etst ref\"\n    ){\n        Id\n        Name\n        GoalAmount\n        GoalTargetDate\n        ImageRef\n    }\n}",
  variables: {}
})
var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  body: graphql,
  redirect: 'follow'
};

fetch("", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Go

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := ""
  method := "GET"

  payload := strings.NewReader("{\"query\":\"mutation{\\n    Create(\\n        request: \\\"Xboxx\\\",\\n        GoalAmount: 123,\\n        GoalTargetData: \\\"15/09/2021\\\",\\n        ImagerRef:\\\"etst ref\\\"\\n    ){\\n        Id\\n        Name\\n        GoalAmount\\n        GoalTargetDate\\n        ImageRef\\n    }\\n}\",\"variables\":{}}")

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
Manny
  • 369
  • 2
  • 7