在 graphql 查询中转义字符格式
Escape character formatting in graphql query
需要帮助格式化 java 代码中的 graphql 突变。在邮递员中它看起来像这样->。postman
我试过的是-> my code and the error I am getting is--> error I am getting。提前致谢:-)
转义将取决于语言,因此我根据您提供的示例在下面提供了几个示例。
Postman 提供了一个以各种格式导出的代码片段
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();
java脚本
//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));
开始
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))
}
需要帮助格式化 java 代码中的 graphql 突变。在邮递员中它看起来像这样->。postman 我试过的是-> my code and the error I am getting is--> error I am getting。提前致谢:-)
转义将取决于语言,因此我根据您提供的示例在下面提供了几个示例。
Postman 提供了一个以各种格式导出的代码片段
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();
java脚本
//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));
开始
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))
}