如何通过graphql和curl上传文件?出现类似F: command not found的错误
curl http://localhost:8888/graphql \
-F operations='{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id, name, content } }", "variables": { "files": [null, null] } }' \
-F map='{ "0": ["variables.files.0"], "1": ["variables.files.1"] }' \
-F 0=@./example/fileupload/testfiles/a.txt \
-F 1=@./example/fileupload/testfiles/c.txt
发布于 2020-04-29 03:24:53
得到了一个使用gqlgen的工作版本,它有一个文件上传示例https://github.com/99designs/gqlgen/tree/master/example/fileupload
并希望通过https://github.com/jaydenseric/graphql-multipart-request-spec备注中的规范来实现这一点。该规范为您提供了一个潜在的项目列表,您可以为每个处理文件上传的服务器和客户端遵循这些项目。
示例curl请求,尽管根据您的实现方式,您可能还需要通过-H包含头部。当你继续另一行时,注意不要在\之后有任何空格,否则你会得到像F: command not found这样的错误。我还包含了-v和-L以获取更多详细信息,这样如果有任何重定向,您仍然可以在控制台中正确地获得输出。
curl -v -L http://localhost:8087/graphql \
-F operations='{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id, name, content } }", "variables": { "files": [null, null] } }' \
-F map='{ "0": ["variables.files.0"], "1": ["variables.files.1"] }' \
-F 0=@./example/fileupload/testfiles/a.txt \
-F 1=@./example/fileupload/testfiles/c.txt
这负责查询服务器,我也可以使用如下设置通过Postman访问它
在客户端,如果使用的是apollo,关键的一点是从https://github.com/jaydenseric/apollo-upload-client添加项目apollo upload- client,在App.js中用createUploadLink({ uri:...})替换HttpLink,然后重新启动客户端。在我这样做之后,示例组件https://github.com/jaydenseric/apollo-upload-examples/blob/master/app/components/UploadFileList.js终于对我起作用了。
即使突变查询说它使用Upload类型,您也不需要为它创建一个接口,它应该从服务器处理(因此,如果您得到类似于can't convert map string interface{} to Upload的错误消息,似乎与实际发生的错误无关)
入门的其他提示:在golang中,当数据加载到graphql查询中时,您可以通过提取内容([]byte)来写入文件,或者可以将文件作为Blob保存到sql服务器。
content, err := ioutil.ReadAll(file.File)
...
err := ioutil.WriteFile("/tmp/outputFile", content, 0644)
在加载文件时,可以将数据作为字符串重新加载,然后以[]字节(file.Content)的形式输入到writeFile函数中。
https://stackoverflow.com/questions/61488358
复制相似问题