Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在HTTP请求中格式化json参数?_Json_Http_Curl_Post - Fatal编程技术网

如何在HTTP请求中格式化json参数?

如何在HTTP请求中格式化json参数?,json,http,curl,post,Json,Http,Curl,Post,我的应用程序从远程服务器检索身份验证令牌。以下请求工作正常,但我想从json文件中读取参数: curl -X POST "https://identity-r.my_office.ch/realms/bfs-sis-r/protocol/openid-connect/token" -H "accept: */*" -H "Content-Type: application/x-www-form-urlencoded" -d &quo

我的应用程序从远程服务器检索身份验证令牌。以下请求工作正常,但我想从json文件中读取参数:

curl -X POST "https://identity-r.my_office.ch/realms/bfs-sis-r/protocol/openid-connect/token" 
-H "accept: */*" -H "Content-Type: application/x-www-form-urlencoded" 
-d "username=dataproducer&password=dataproducer&grant_type=password&client_id=BFS.SIS&scope=openid profile roles email"
我创建了这个json文件来存储参数:

credentials.json

{"username": "dataproducer", 
"password": "dataproducer", 
"grant_type": "password", 
"client_id": "BFS.SIS", 
"scope": "openid profile roles email"
}
我尝试在请求中使用它:

curl -X POST "https://identity-r.my_office.ch/realms/bfs-sis-r/protocol/openid-connect/token" 
-H "accept: */*" -H "Content-type: application/json" -d @credentials.json
但答案是肯定的

{“错误”:“RESTEASY003065:无法使用内容类型”}

你能帮我正确格式化这个请求吗?
非常感谢

如果您尝试以下代码:

curl -H "Accept: */*" -H "Content-type: application/json" -X POST --data "@path_of_file" https://identity-r.my_office.ch/realms/bfs-sis-r/protocol/openid-connect/token
或使用字符集发送-H:

curl -H "Accept: */*" -H "Content-type: application/json; charset=UTF-8" -X POST --data "@path_of_file" https://identity-r.my_office.ch/realms/bfs-sis-r/protocol/openid-connect/token

您还可以更改并尝试将
--data
更改为
--data binary
。不同的是,
--data
会导致curl去掉换行符,但是
--data binary
不会。不幸的是,所有这些都没有效果:(你确定服务器会接受JSON作为发布数据吗?是的。但是我发送的JSON似乎包含转义字符,比如\“而不是简单的”在数据周围,这会导致错误。我可以大摇大摆地测试它……上面显示的“credentials.json”中没有转义的双引号,所以这是不可能的。