可以通过在发送请求时设置'Content-Type”头部参数为'text/plain”或类似的值来取消默认的JSON序列化设置,以便发送字符串格式。
示例代码:
使用Axios库发送请求:
import axios from 'axios';
const data = 'example string';
axios.post('/api/post', data, {
headers: {
'Content-Type': 'text/plain'
}
})
使用GraphQLClient发送GraphQL请求:
import { GraphQLClient } from 'graphql-request';
const data = 'example string';
const client = new GraphQLClient(endpoint, {
headers: {
'Content-Type': 'text/plain'
}
});
const query = `
mutation ExampleMutation($data: String!) {
exampleMutation(data: $data) {
id
}
}
`;
client.request(query, { data });