Shellscriptカールがエラーを発生させる

Shellscriptカールがエラーを発生させる

カールを使用して小さなシェルスクリプトを作成しようとしています。

スクリプトは次のとおりです。 (ユーザー名、URL、およびCo.を非表示にする必要があります。)

for i in `cat cain.txt`
do
uuid=$(curl   -X POST -H "ACCEPT-LANGUAGE:en"   -H "Content-Type:application/json"   -H "Accept: application/json" -d  {"username":"TheUserName"}  HereTheUrl) 
> final.txt
fuid=$(echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p' >>  final.txt)
eval $fuid
echo $fuid

service_uuid=$(cat final.txt)
password=$(sed -n 1p cain.txt)
echo $service_uuid
echo $password

login=$(curl -X POST -H "ACCEPT-LANGUAGE:en"  -H "Content-Type: application/json;charset=utf-8"  -H "Accept-Encoding:gzip,deflate"   -H "Accept: application/json"  -H "Connection: keep-alive" -d  {"serviceUuid":"${service_uuid}","password":"${password}"}  HereTheUrl)

echo "$login"
if [ "$login" = "STATUS_OK" ]
then
echo "Password: $password"
break
else
echo "Trying: $password"
fi
done

これでこれを実行すると、次のエラーが発生します。

{"status":{"code":"SERVER_ERROR","message":"java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"},"body":{"data":{"detailMessage":"java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $","cause":{"detailMessage":"Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $","stackTrace":[{"declaringClass":"com.google.gson.stream.JsonReader","methodName":"beginObject","fileName":"JsonReader.java","lineNumber":385},{"declaringClass":"com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter","methodName":"read","fileName":"ReflectiveTypeAdapterFactory.java","lineNumber":213},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":887},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":852},{"declaringClass":"com.teamdae.common.arch.jax.handler.GsonMessageBodyHandler","methodName":"readFrom","fileName":"GsonMessageBodyHandler.java","lineNumber":91},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBodyReader","fileName":"JAXRSUtils.java","lineNumber":1342},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBody","fileName":"JAXRSUtils.java","lineNumber":1293},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameter","fileName":"JAXRSUtils.java","lineNumber":826},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameters","fileName":"JAXRSUtils.java","lineNumber":789},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"processRequest","fileName":"JAXRSInInterceptor.java","lineNumber":212},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"handleMessage","fileName":"JAXRSInInterceptor.java","lineNumber":77},{"declaringClass":"org.apache.cxf.phase.PhaseInterceptorChain","methodName":"doIntercept","fileName":"PhaseInterceptorChain.java","lineNumber":308},{"declaringClass":"org.apache.cxf.transport.ChainInitiationObserver","methodName":"onMessage","fileName":"ChainInitiationObserver.java","lineNumber":121},{"declaringClass":"org.apache.cxf.transport.http.AbstractHTTPDestination","methodName":"invoke","fileName":"AbstractHTTPDestination.java","lineNumber":254},{"declaringClass":"org.apache.openejb.server.cxf.rs.CxfRsHttpListener","methodName":"doInvoke","fileName":"CxfRsHttpListener.java","lineNumber":251},{"declaringClass":"org.apache.tomee.webservices.CXFJAXRSFilter","methodName":"doFilter","fileName":"CXFJAXRSFilter.java","lineNumber":94},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.tomcat.websocket.server.WsFilter","methodName":"doFilter","fileName":"WsFilter.java","lineNumber":52},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.filters.HttpHeaderSecurityFilter","methodName":"doFilter","fileName":"HttpHeaderSecurityFilter.java","lineNumber":120},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.core.StandardWrapperValve","methodName":"invoke","fileName":"StandardWrapperValve.java","lineNumber":198},{"declaringClass":"org.apache.catalina.core.StandardContextValve","methodName":"invoke","fileName":"StandardContextValve.java","lineNumber":108},{"declaringClass":"org.apache.tomee.catalina.OpenEJBValve","methodName":"invoke","fileName":"OpenEJBValve.java","lineNumber":44},{"declaringClass":"org.apache.catalina.authenticator.AuthenticatorBase","methodName":"invoke","fileName":"AuthenticatorBase.java","lineNumber":522},{"declaringClass":"org.apache.catalina.core.StandardHostValve","methodName":"invoke","fileName":"StandardHostValve.java","lineNumber":140},{"declaringClass":"org.apache.catalina.valves.ErrorReportValve","methodName":"invoke","fileName":"ErrorReportValve.java","lineNumber":79},{"declaringClass":"org.apache.catalina.valves.AbstractAccessLogValve","methodName":"invoke","fileName":"AbstractAccessLogValve.java","lineNumber":620},{"declaringClass":"org.apache.catalina.core.StandardEngineValve","methodName":"invoke","fileName":"StandardEngineValve.java","lineNumber":87},{"declaringClass":"org.apache.catalina.connector.CoyoteAdapter","methodName":"service","fileName":"CoyoteAdapter.java","lineNumber":349},{"declaringClass":"org.apache.coyote.ajp.AjpProcessor","methodName":"service","fileName":"AjpProcessor.java","lineNumber":780},{"declaringClass":"org.apache.coyote.AbstractProcessorLight","methodName":"process","fileName":"AbstractProcessorLight.java","lineNumber":66},{"declaringClass":"org.apache.coyote.AbstractProtocol$ConnectionHandler","methodName":"process","fileName":"AbstractProtocol.java","lineNumber":785},{"declaringClass":"org.apache.tomcat.util.net.NioEndpoint$SocketProcessor","methodName":"doRun","fileName":"NioEndpoint.java","lineNumber":1425},{"declaringClass":"org.apache.tomcat.util.net.SocketProcessorBase","methodName":"run","fileName":"SocketProcessorBase.java","lineNumber":52},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor","methodName":"runWorker","fileName":"ThreadPoolExecutor.java","lineNumber":1142},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor$Worker","methodName":"run","fileName":"ThreadPoolExecutor.java","lineNumber":617},{"declaringClass":"org.apache.tomcat.util.threads.TaskThread$WrappingRunnable","methodName":"run","fileName":"TaskThread.java","lineNumber":61},{"declaringClass":"java.lang.Thread","methodName":"run","fileName":"Thread.java","lineNumber":745}],"suppressedExceptions":[]},"stackTrace":[{"declaringClass":"com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter","methodName":"read","fileName":"ReflectiveTypeAdapterFactory.java","lineNumber":224},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":887},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":852},{"declaringClass":"com.teamdae.common.arch.jax.handler.GsonMessageBodyHandler","methodName":"readFrom","fileName":"GsonMessageBodyHandler.java","lineNumber":91},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBodyReader","fileName":"JAXRSUtils.java","lineNumber":1342},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBody","fileName":"JAXRSUtils.java","lineNumber":1293},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameter","fileName":"JAXRSUtils.java","lineNumber":826},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameters","fileName":"JAXRSUtils.java","lineNumber":789},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"processRequest","fileName":"JAXRSInInterceptor.java","lineNumber":212},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"handleMessage","fileName":"JAXRSInInterceptor.java","lineNumber":77},{"declaringClass":"org.apache.cxf.phase.PhaseInterceptorChain","methodName":"doIntercept","fileName":"PhaseInterceptorChain.java","lineNumber":308},{"declaringClass":"org.apache.cxf.transport.ChainInitiationObserver","methodName":"onMessage","fileName":"ChainInitiationObserver.java","lineNumber":121},{"declaringClass":"org.apache.cxf.transport.http.AbstractHTTPDestination","methodName":"invoke","fileName":"AbstractHTTPDestination.java","lineNumber":254},{"declaringClass":"org.apache.openejb.server.cxf.rs.CxfRsHttpListener","methodName":"doInvoke","fileName":"CxfRsHttpListener.java","lineNumber":251},{"declaringClass":"org.apache.tomee.webservices.CXFJAXRSFilter","methodName":"doFilter","fileName":"CXFJAXRSFilter.java","lineNumber":94},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.tomcat.websocket.server.WsFilter","methodName":"doFilter","fileName":"WsFilter.java","lineNumber":52},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.filters.HttpHeaderSecurityFilter","methodName":"doFilter","fileName":"HttpHeaderSecurityFilter.java","lineNumber":120},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.core.StandardWrapperValve","methodName":"invoke","fileName":"StandardWrapperValve.java","lineNumber":198},{"declaringClass":"org.apache.catalina.core.StandardContextValve","methodName":"invoke","fileName":"StandardContextValve.java","lineNumber":108},{"declaringClass":"org.apache.tomee.catalina.OpenEJBValve","methodName":"invoke","fileName":"OpenEJBValve.java","lineNumber":44},{"declaringClass":"org.apache.catalina.authenticator.AuthenticatorBase","methodName":"invoke","fileName":"AuthenticatorBase.java","lineNumber":522},{"declaringClass":"org.apache.catalina.core.StandardHostValve","methodName":"invoke","fileName":"StandardHostValve.java","lineNumber":140},{"declaringClass":"org.apache.catalina.valves.ErrorReportValve","methodName":"invoke","fileName":"ErrorReportValve.java","lineNumber":79},{"declaringClass":"org.apache.catalina.valves.AbstractAccessLogValve","methodName":"invoke","fileName":"AbstractAccessLogValve.java","lineNumber":620},{"declaringClass":"org.apache.catalina.core.StandardEngineValve","methodName":"invoke","fileName":"StandardEngineValve.java","lineNumber":87},{"declaringClass":"org.apache.catalina.connector.CoyoteAdapter","methodName":"service","fileName":"CoyoteAdapter.java","lineNumber":349},{"declaringClass":"org.apache.coyote.ajp.AjpProcessor","methodName":"service","fileName":"AjpProcessor.java","lineNumber":780},{"declaringClass":"org.apache.coyote.AbstractProcessorLight","methodName":"process","fileName":"AbstractProcessorLight.java","lineNumber":66},{"declaringClass":"org.apache.coyote.AbstractProtocol$ConnectionHandler","methodName":"process","fileName":"AbstractProtocol.java","lineNumber":785},{"declaringClass":"org.apache.tomcat.util.net.NioEndpoint$SocketProcessor","methodName":"doRun","fileName":"NioEndpoint.java","lineNumber":1425},{"declaringClass":"org.apache.tomcat.util.net.SocketProcessorBase","methodName":"run","fileName":"SocketProcessorBase.java","lineNumber":52},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor","methodName":"runWorker","fileName":"ThreadPoolExecutor.java","lineNumber":1142},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor$Worker","methodName":"run","fileName":"ThreadPoolExecutor.java","lineNumber":617},{"declaringClass":"org.apache.tomcat.util.threads.TaskThread$WrappingRunnable","methodName":"run","fileName":"TaskThread.java","lineNumber":61},{"declaringClass":"java.lang.Thread","methodName":"run","fileName":"Thread.java","lineNumber":745}],"suppressedExceptions":[]}}}

答え1

私の考えでは、データ(ペイロード)をよく参照していなかったためだと思います。

>echo {"username":"TheUserName"} 
{username:TheUserName}

二重引用符はシェルで処理した後に消えます。

AFAIK JSONは引用符で囲まれていない文字列を受け入れません。

この問題を解決するには、次のようにデータの周りに一重引用符ペアを追加します。

>echo '{"username":"TheUserName"}'
{"username":"TheUserName"}

シェル拡張(変数の置換)がデータで機能するようにするには、二重引用符を使用して内部引用符をエスケープします。

>echo "{\"username\":\"$username\"}"
{"username":"myuser"}

シェルパラメータを正しく引用するより多くの方法があることに注意してください(このサイトにはすでにこれに対する良い答えがたくさんあると思います)。必要に応じてデータをファイルに入れることもできます。

関連情報