在命令行中使用 curl 通过 JSON 对象发布数据
Posted
技术标签:
【中文标题】在命令行中使用 curl 通过 JSON 对象发布数据【英文标题】:Posting data via JSON object using curl in the command line 【发布时间】:2019-10-21 15:59:57 【问题描述】:我正在尝试通过命令行将数据从一台服务器发布到另一台服务器。最终,我将使用真实数据,但现在我只想测试它是否有效,但它没有。
在 server1 的命令行中,我使用 curl 通过 JSON 发送数据,而在我的其他服务器中,我有一个方法可以接收 JSON 对象,对其进行解码并打印出来,但是这里有问题。
不发送 JSON 对象,一切正常:我可以打印“例句”,这表明我的方法正在被访问并且有效。
但是,当我发送 JSON 对象时,它会打印我的 .env 文件中的所有详细信息,而我发送的对象中没有任何内容,我不明白。
我尝试过用很多不同的方式编写 curl,但似乎没有任何效果。
来自服务器 1 的卷曲:
curl -X POST -H "Content-Type: application/json" \
-d '"orderId":12345,"customerNr":98765' \
https://returns.jdoe.blah.test/createReturn
来自服务器 2 上 web.php 的路由:
Route::post('/createReturn', 'ProductReturnsController@createReturn');
来自服务器 2 上控制器 ProductReturnsController.php 的方法:
public function createReturn($json)
echo "hallo\n";
$jsonDecoded = json_decode($json);
echo $jsonDecoded->orderId;
print_r($jsonDecoded);
//return view('data');
我现在已将视图注释掉,因为我想确保我可以先发送和接收 JSON 对象。
我在命令行运行curl后的输出如下:
>"<span class=sf-dump-str title="3 characters">120</span>"
</pre><script>Sfdump("sf-dump-686665084")</script>
</td>
</tr>
<tr>
<td>REDIS_HOST</td>
<td><pre class=sf-dump id=sf-dump-15249655 data-indent-pad=" ">"<span class=sf-dump-str title="9 characters">127.0.0.1</span>"
</pre><script>Sfdump("sf-dump-15249655")</script>
</td>
</tr>
<tr>
<td>REDIS_PASSWORD</td>
<td><pre class=sf-dump id=sf-dump-1240246613 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">null</span>"
</pre><script>Sfdump("sf-dump-1240246613")</script>
</td>
</tr>
<tr>
<td>REDIS_PORT</td>
<td><pre class=sf-dump id=sf-dump-2128112850 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">6379</span>"
</pre><script>Sfdump("sf-dump-2128112850")</script>
</td>
</tr>
<tr>
<td>MAIL_DRIVER</td>
<td><pre class=sf-dump id=sf-dump-89924640 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">smtp</span>"
</pre><script>Sfdump("sf-dump-89924640")</script>
</td>
</tr>
<tr>
<td>MAIL_HOST</td>
<td><pre class=sf-dump id=sf-dump-298105600 data-indent-pad=" ">"<span class=sf-dump-str title="16 characters">smtp.mailtrap.io</span>"
</pre><script>Sfdump("sf-dump-298105600")</script>
</td>
</tr>
<tr>
<td>MAIL_PORT</td>
<td><pre class=sf-dump id=sf-dump-1783830794 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">2525</span>"
</pre><script>Sfdump("sf-dump-1783830794")</script>
</td>
</tr>
<tr>
<td>MAIL_USERNAME</td>
<td><pre class=sf-dump id=sf-dump-18572966 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">null</span>"
</pre><script>Sfdump("sf-dump-18572966")</script>
</td>
</tr>
<tr>
<td>MAIL_PASSWORD</td>
<td><pre class=sf-dump id=sf-dump-1965143115 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">null</span>"
</pre><script>Sfdump("sf-dump-1965143115")</script>
</td>
</tr>
<tr>
<td>MAIL_ENCRYPTION</td>
<td><pre class=sf-dump id=sf-dump-384128930 data-indent-pad=" ">"<span class=sf-dump-str title="4 characters">null</span>"
</pre><script>Sfdump("sf-dump-384128930")</script>
</td>
</tr>
<tr>
<td>AWS_ACCESS_KEY_ID</td>
<td><pre class=sf-dump id=sf-dump-1978382159 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-1978382159")</script>
</td>
</tr>
<tr>
<td>AWS_SECRET_ACCESS_KEY</td>
<td><pre class=sf-dump id=sf-dump-593039413 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-593039413")</script>
</td>
</tr>
<tr>
<td>AWS_DEFAULT_REGION</td>
<td><pre class=sf-dump id=sf-dump-1272900874 data-indent-pad=" ">"<span class=sf-dump-str title="9 characters">us-east-1</span>"
</pre><script>Sfdump("sf-dump-1272900874")</script>
</td>
</tr>
<tr>
<td>AWS_BUCKET</td>
<td><pre class=sf-dump id=sf-dump-1804584219 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-1804584219")</script>
</td>
</tr>
<tr>
<td>PUSHER_APP_ID</td>
<td><pre class=sf-dump id=sf-dump-95334387 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-95334387")</script>
</td>
</tr>
<tr>
<td>PUSHER_APP_KEY</td>
<td><pre class=sf-dump id=sf-dump-1487081807 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-1487081807")</script>
</td>
</tr>
<tr>
<td>PUSHER_APP_SECRET</td>
<td><pre class=sf-dump id=sf-dump-1935165849 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-1935165849")</script>
</td>
</tr>
<tr>
<td>PUSHER_APP_CLUSTER</td>
<td><pre class=sf-dump id=sf-dump-1333546276 data-indent-pad=" ">"<span class=sf-dump-str title="3 characters">mt1</span>"
</pre><script>Sfdump("sf-dump-1333546276")</script>
</td>
</tr>
<tr>
<td>MIX_PUSHER_APP_KEY</td>
<td><pre class=sf-dump id=sf-dump-1872466253 data-indent-pad=" ">""
</pre><script>Sfdump("sf-dump-1872466253")</script>
</td>
</tr>
<tr>
<td>MIX_PUSHER_APP_CLUSTER</td>
<td><pre class=sf-dump id=sf-dump-1866771313 data-indent-pad=" ">"<span class=sf-dump-str title="3 characters">mt1</span>"
</pre><script>Sfdump("sf-dump-1866771313")</script>
</td>
</tr>
<tr>
<td>LDAP_USERNAME</td>
<td><pre class=sf-dump id=sf-dump-2006453280 data-indent-pad=" ">"<span class=sf-dump-str title="18 characters">jdoe@ad.blah.de</span>"
</pre><script>Sfdump("sf-dump-2006453280")</script>
</td>
</tr>
<tr>
<td>LDAP_PASSWORD</td>
<td><pre class=sf-dump id=sf-dump-1985838971 data-indent-pad="
">"<span class=sf-dump-str title="10 characters">XXXXXXXXXX</span>"
</pre><script>Sfdump("sf-dump-1985838971")</script>
</td>
</tr>
</table>
</div>
</div>
<div class="data-table-container" id="handlers">
<label>Registered Handlers</label>
<div class="handler active">
0. Whoops\Handler\PrettyPageHandler </div>
</div>
</div>
</div>
</div>
</div>
<script>var r=null;window.PR_SHOULD_USE_CONTINUATION=!0;
(function()function O(a)function i(d)var a=d.charCodeAt(0);if(a!==92)return a;var f=d.charAt(1);return(a=s[f])?a:"0"<=f&&f<="7"?parseInt(d.substring(1),8):f==="u"||f==="x"?parseInt(d.substring(2),16):d.charCodeAt(1)function g(d)if(d<32)return(d<16?"\\x0":"\\x")+d.toString(16);d=String.fromCharCode(d);return d==="\\"||d==="-"||d==="]"||d==="^"?"\\"+d:dfunction j(d)var a=d.substring(1,d.length-1).match(/\\u[\dA-Fa-f]4|\\x[\dA-Fa-f]2|\\[0-3][0-7]0,2|\\[0-7]1,2|\\[\S\s]|[^\\]/g),d=[],f=
a[0]==="^",b=["["];f&&b.push("^");for(var f=f?1:0,c=a.length;f<c;++f)var i=this.attr("data-"+e.replace(m,"-$1").toLowerCase(),n);return null!==i?Y(i):t,val:function(t)return 0===arguments.length?this[0]&&(this[0].multiple?n(this[0]).find("option").filter(function()return this.selected).pluck("value"):this[0].value):this.each(function(e)this.value=J(this,t,e,this.value)),offset:function(t)if(t)return this.each(function(e)var i=n(this),r=J(this,t,e,i.offset()),o=i.offsetParent().offset(),s=top:r.top-o.top,left:r.left-o.left;"static"==i.css("position")&&(s.position="relative"),i.css(s));if(0==this.length)return null;var e=this[0].getBoundingClientRect();returnleft:e.left+window.pageXOffset,top:e.top+window.pageYOffset,width:Math.round(e.width),height:Math.round(e.height),css:function(t,i)if(arguments.length<2)var r=this[0],o=getComputedStyle(r,"");if(!r)return;if("string"==typeof t)return r.style[C(t)]||o.getPropertyValue(t);if(A(t))var s=;return n.each(A(t)?t:[t],function(t,e)s[e]=r.style[C(e)]||o.getPropertyValue(e)),svar a="";if("string"==L(t))i||0===i?a=F(t)+":"+H(t,i):this.each(function()this.style.removeProperty(F(t)));else for(e in t)t[e]||0===t[e]?a+=F(e)+":"+H(e,t[e])+";":this.each(function()this.style.removeProperty(F(e)));return this.each(function()this.style.cssText+=";"+a),index:function(t)return t?this.indexOf(n(t)[0]):this.parent().children().indexOf(this[0]),hasClass:function(t)return t?r.some.call(this,function(t)return this.test(W(t)),q(t)):!1,addClass:function(t)return t?this.each(function(e)i=[];var r=W(this),o=J(this,t,e,r);o.split(/\s+/g).forEach(function(t)n(this).hasClass(t)||i.push(t),this),i.length&&W(this,r+(r?" ":"")+i.join(" "))):this,removeClass:function(e)return this.each(function(n)return e===t?W(this,""):(i=W(this),J(this,e,n,i).split(/\s+/g).forEach(function(t)i=i.replace(q(t)," ")),void W(this,i.trim()))),toggleClass:function(e,i)return e?this.each(function(r)var o=n(this),s=J(this,e,r,W(this));s.split(/\s+/g).forEach(function(e)(i===t?!o.hasClass(e):i)?o.addClass(e):o.removeClass(e))):this,scrollTop:function(e)if(this.length)var n="scrollTop"in this[0];return e===t?n?this[0].scrollTop:this[0].pageYOffset:this.each(n?function()this.scrollTop=e:function()this.scrollTo(this.scrollX,e)),scrollLeft:function(e)if(this.length)var n="scrollLeft"in this[0];return e===t?n?this[0].scrollLeft:this[0].pageXOffset:this.each(n?function()this.scrollLeft=e:function()this.scrollTo(e,this.scrollY)),position:function()if(this.length)var t=this[0],e=this.offsetParent(),i=this.offset(),r=d.test(e[0].nodeName)?top:0,left:0:e.offset();return i.top-=parseFloat(n(t).css("margin-top"))||0,i.left-=parseFloat(n(t).css("margin-left"))||0,r.top+=parseFloat(n(e[0]).css("border-top-width"))||0,r.left+=parseFloat(n(e[0]).css("border-left-width"))||0,top:i.top-r.top,left:i.left-r.left,offsetParent:function()return this.map(function()for(var t=this.offsetParent||a.body;t&&!d.test(t.nodeName)&&"static"==n(t).css("position");)t=t.offsetParent;return t),n.fn.detach=n.fn.remove,["width","height"].forEach(function(e)var i=e.replace(/./,function(t)return t[0].toUpperCase());n.fn[e]=function(r)var o,s=this[0];return r===t?$(s)?s["inner"+i]:_(s)?s.documentElement["scroll"+i]:(o=this.offset())&&o[e]:this.each(function(t)s=n(this),s.css(e,J(this,r,t,s[e]())))),v.forEach(function(t,e)var i=e%2;n.fn[t]=function()var t,o,r=n.map(arguments,function(e)return t=L(e),"object"==t||"array"==t||null==e?e:S.fragment(e)),s=this.length>1;return r.length<1?this:this.each(function(t,a)o=i?a:a.parentNode,a=0==e?a.nextSibling:1==e?a.firstChild:2==e?a:null,r.forEach(function(t)if(s)t=t.cloneNode(!0);else if(!o)return n(t).remove();G(o.insertBefore(t,a),function(t)null==t.nodeName||"SCRIPT"!==t.nodeName.toUpperCase()||t.type&&"text/javascript"!==t.type||t.src||window.eval.call(window,t.innerhtml)))),n.fn[i?t+"To":"insert"+(e?"Before":"After")]=function(e)return n(e)[t](this),this),S.Z.prototype=n.fn,S.uniq=N,S.deserializeValue=Y,n.zepto=S,n();window.Zepto=Zepto,void 0===window.$&&(window.$=Zepto),function(t)function l(t)return t._zid||(t._zid=e++)function h(t,e,n,i)if(e=p(e),e.ns)var r=d(e.ns);return(s[l(t)]||[]).filter(function(t)return!(!t||e.e&&t.e!=e.e||e.ns&&!r.test(t.ns)||n&&l(t.fn)!==l(n)||i&&t.sel!=i))function p(t)var e=(""+t).split(".");returne:e[0],ns:e.slice(1).sort().join(" ")function d(t)return new RegExp("(?:^| )"+t.replace(" "," .* ?")+"(?: |$)")function m(t,e)return t.del&&!u&&t.e in f||!!efunction g(t)return c[t]||u&&f[t]||tfunction v(e,i,r,o,a,u,f)var h=l(e),d=s[h]||(s[h]=[]);i.split(/\s/).forEach(function(i)if("ready"==i)return t(document).ready(r);var s=p(i);s.fn=r,s.sel=a,s.e in c&&(r=function(e)var n=e.relatedTarget;return!n||n!==this&&!t.contains(this,n)?s.fn.apply(this,arguments):void 0),s.del=u;var l=u||r;s.proxy=function(t)if(t=j(t),!t.isImmediatePropagationStopped())t.data=o;var i=l.apply(e,t._args==n?[t]:[t].concat(t._args));return i===!1&&(t.preventDefault(),t.stopPropagation()),i,s.i=d.length,d.push(s),"addEventListener"in e&&e.addEventListener(g(s.e),s.proxy,m(s,f)))function y(t,e,n,i,r)var o=l(t);(e||"").split(/\s/).forEach(function(e)h(t,e,n,i).forEach(function(e)delete s[o][e.i],"removeEventListener"in t&&t.removeEventListener(g(e.e),e.proxy,m(e,r))))function j(e,i)return(i||!e.isDefaultPrevented)&&(i||(i=e),t.each(E,function(t,n)var r=i[t];e[t]=function()return this[n]=x,r&&r.apply(i,arguments),e[n]=b),(i.defaultPrevented!==n?i.defaultPrevented:"returnValue"in i?i.returnValue===!1:i.getPreventDefault&&i.getPreventDefault())&&(e.isDefaultPrevented=x)),efunction T(t)var e,i=originalEvent:t;for(e in t)w.test(e)||t[e]===n||(i[e]=t[e]);return j(i,t)var n,e=1,i=Array.prototype.slice,r=t.isFunction,o=function(t)return"string"==typeof t,s=,a=,u="onfocusin"in window,f=focus:"focusin",blur:"focusout",c=mouseenter:"mouseover",mouseleave:"mouseout";a.click=a.mousedown=a.mouseup=a.mousemove="MouseEvents",t.event=add:v,remove:y,t.proxy=function(e,n)if(r(e))var i=function()return e.apply(n,arguments);return i._zid=l(e),iif(o(n))return t.proxy(e[n],e);throw new TypeError("expected function"),t.fn.bind=function(t,e,n)return this.on(t,e,n),t.fn.unbind=function(t,e)return this.off(t,e),t.fn.one=function(t,e,n,i)return this.on(t,e,n,i,1);var x=function()return!0,b=function()return!1,w=/^([A-Z]|returnValue$|layer[XY]$)/,E=preventDefault:"isDefaultPrevented",stopImmediatePropagation:"isImmediatePropagationStopped",stopPropagation:"isPropagationStopped";t.fn.delegate=function(t,e,n)return this.on(e,t,n),t.fn.undelegate=function(t,e,n)return this.off(e,t,n),t.fn.live=function(e,n)return t(document.body).delegate(this.selector,e,n),this,t.fn.die=function(e,n)return t(document.body).undelegate(this.selector,e,n),this,t.fn.on=function(e,s,a,u,f)var c,l,h=this;return e&&!o(e)?(t.each(e,function(t,e)h.on(t,s,a,e,f)),h):(o(s)||r(u)||u===!1||(u=a,a=s,s=n),(r(a)||a===!1)&&(u=a,a=n),u===!1&&(u=b),h.each(function(n,r)f&&(c=function(t)return y(r,t.type,u),u.apply(this,arguments)),s&&(l=function(e)var n,o=t(e.target).closest(s,r).get(0);return o&&o!==r?(n=t.extend(T(e),currentTarget:o,liveFired:r),(c||u).apply(o,[n].concat(i.call(arguments,1)))):void 0),v(r,e,u,a,s,l||c))),t.fn.off=function(e,i,s)var a=this;return e&&!o(e)?(t.each(e,function(t,e)a.off(t,i,e)),a):(o(i)||r(s)||s===!1||(s=i,i=n),s===!1&&(s=b),a.each(function()y(this,e,s,i))),t.fn.trigger=function(e,n)return e=o(e)||t.isPlainObject(e)?t.Event(e):j(e),e._args=n,this.each(function()"dispatchEvent"in this?this.dispatchEvent(e):t(this).triggerHandler(e,n)),t.fn.triggerHandler=function(e,n)var i,r;return this.each(function(s,a)i=T(o(e)?t.Event(e):e),i._args=n,i.target=a,t.each(h(a,e.type||e),function(t,e)return r=e.proxy(i),i.isImmediatePropagationStopped()?!1:void 0)),r,"focusin focusout load resize scroll unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select keydown keypress keyup error".split(" ").forEach(function(e)t.fn[e]=function(t)return t?this.bind(e,t):this.trigger(e)),["focus","blur"].forEach(function(e)t.fn[e]=function(t)return t?this.bind(e,t):this.each(function()trythis[e]()catch(t)),this),t.Event=function(t,e)o(t)||(e=t,t=e.type);var n=document.createEvent(a[t]||"Events"),i=!0;if(e)for(var r in e)"bubbles"==r?i=!!e[r]:n[r]=e[r];return n.initEvent(t,i,!0),j(n)(Zepto),function(t)function l(e,n,i)var r=t.Event(n);return t(e).trigger(r,i),!r.isDefaultPrevented()function h(t,e,i,r)return t.global?l(e||n,i,r):void 0function p(e)e.global&&0===t.active++&&h(e,null,"ajaxStart")function d(e)e.global&&!--t.active&&h(e,null,"ajaxStop")function m(t,e)var n=e.context;return e.beforeSend.call(n,t,e)===!1||h(e,n,"ajaxBeforeSend",[t,e])===!1?!1:void h(e,n,"ajaxSend",[t,e])function g(t,e,n,i)var r=n.context,o="success";n.success.call(r,t,o,e),i&&i.resolveWith(r,[t,o,e]),h(n,r,"ajaxSuccess",[e,n,t]),y(o,e,n)function v(t,e,n,i,r)var o=i.context;i.error.call(o,n,e,t),r&&r.rejectWith(o,[n,e,t]),h(i,o,"ajaxError",[n,i,t||e]),y(e,n,i)function y(t,e,n)var i=n.context;n.complete.call(i,e,t),h(n,i,"ajaxComplete",[e,n]),d(n)function x()function b(t)return t&&(t=t.split(";",2)[0]),t&&(t==f?"html":t==u?"json":s.test(t)?"script":a.test(t)&&"xml")||"text"function w(t,e)return""==e?t:(t+"&"+e).replace(/[&?]1,2/,"?")function E(e)e.processData&&e.data&&"string"!=t.type(e.data)&&(e.data=t.param(e.data,e.traditional)),!e.data||e.type&&"GET"!=e.type.toUpperCase()||(e.url=w(e.url,e.data),e.data=void 0)function j(e,n,i,r)return t.isFunction(n)&&(r=i,i=n,n=void 0),t.isFunction(i)||(r=i,i=void 0),url:e,data:n,success:i,dataType:rfunction S(e,n,i,r)var o,s=t.isArray(n),a=t.isPlainObject(n);t.each(n,function(n,u)o=t.type(u),r&&(n=i?r:r+"["+(a||"object"==o||"array"==o?n:"")+"]"),!r&&s?e.add(u.name,u.value):"array"==o||!i&&"object"==o?S(e,u,i,n):e.add(n,u))var i,r,e=0,n=window.document,o=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,s=/^(?:text|application)\/javascript/i,a=/^(?:text|application)\/xml/i,u="application/json",f="text/html",c=/^\s*$/;t.active=0,t.ajaxJSONP=function(i,r)if(!("type"in i))return t.ajax(i);var f,h,o=i.jsonpCallback,s=(t.isFunction(o)?o():o)||"jsonp"+ ++e,a=n.createElement("script"),u=window[s],c=function(e)t(a).triggerHandler("error",e||"abort"),l=abort:c;return r&&r.promise(l),t(a).on("load error",function(e,n)clearTimeout(h),t(a).off().remove(),"error"!=e.type&&f?g(f[0],l,i,r):v(null,n||"error",l,i,r),window[s]=u,f&&t.isFunction(u)&&u(f[0]),u=f=void 0),m(l,i)===!1?(c("abort"),l):(window[s]=function()f=arguments,a.src=i.url.replace(/\?(.+)=\?/,"?$1="+s),n.head.appendChild(a),i.timeout>0&&(h=setTimeout(function()c("timeout"),i.timeout)),l),t.ajaxSettings=type:"GET",beforeSend:x,success:x,error:x,complete:x,context:null,global:!0,xhr:function()return new window.XMLHttpRequest,accepts:script:"text/javascript, application/javascript, application/x-javascript",json:u,xml:"application/xml, text/xml",html:f,text:"text/plain",crossDomain:!1,timeout:0,processData:!0,cache:!0,t.ajax=function(e)var n=t.extend(,e||),o=t.Deferred&&t.Deferred();for(i in t.ajaxSettings)void 0===n[i]&&(n[i]=t.ajaxSettings[i]);p(n),n.crossDomain||(n.crossDomain=/^([\w-]+:)?\/\/([^\/]+)/.test(n.url)&&RegExp.$2!=window.location.host),n.url||(n.url=window.location.toString()),E(n),n.cache===!1&&(n.url=w(n.url,"_="+Date.now()));var s=n.dataType,a=/\?.+=\?/.test(n.url);if("jsonp"==s||a)return a||(n.url=w(n.url,n.jsonp?n.jsonp+"=?":n.jsonp===!1?"":"callback=?")),t.ajaxJSONP(n,o);var j,u=n.accepts[s],f=,l=function(t,e)f[t.toLowerCase()]=[t,e],h=/^([\w-]+:)\/\//.test(n.url)?RegExp.$1:window.location.protocol,d=n.xhr(),y=d.setRequestHeader;if(o&&o.promise(d),n.crossDomain||l("X-Requested-With","XMLHttpRequest"),l("Accept",u||"*/*"),(u=n.mimeType||u)&&(u.indexOf(",")>-1&&(u=u.split(",",2)[0]),d.overrideMimeType&&d.overrideMimeType(u)),(n.contentType||n.contentType!==!1&&n.data&&"GET"!=n.type.toUpperCase())&&l("Content-Type",n.contentType||"application/x-www-form-urlencoded"),n.headers)for(r in n.headers)l(r,n.headers[r]);if(d.setRequestHeader=l,d.onreadystatechange=function()if(4==d.readyState)d.onreadystatechange=x,clearTimeout(j);var e,i=!1;if(d.status>=200&&d.status<300||304==d.status||0==d.status&&"file:"==h)s=s||b(n.mimeType||d.getResponseHeader("content-type")),e=d.responseText;try"script"==s?(1,eval)(e):"xml"==s?e=d.responseXML:"json"==s&&(e=c.test(e)?null:t.parseJSON(e))catch(r)i=ri?v(i,"parsererror",d,n,o):g(e,d,n,o)else v(d.statusText||null,d.status?"error":"abort",d,n,o),m(d,n)===!1)return d.abort(),v(null,"abort",d,n,o),d;if(n.xhrFields)for(r in n.xhrFields)d[r]=n.xhrFields[r];var T="async"in n?n.async:!0;d.open(n.type,n.url,T,n.username,n.password);for(r in f)y.apply(d,f[r]);return n.timeout>0&&(j=setTimeout(function()e=e||[],t.extend(e,t.fn),e.selector=n||"",e.__Z=!0,e,isZ:function(e)return"array"===t.type(e)&&"__Z"in e);trygetComputedStyle(void 0)catch(e)var n=getComputedStyle;window.getComputedStyle=function(t)tryreturn n(t)catch(e)return null(Zepto);
</script>
<script>/*!
* clipboard.js v1.5.3
* https://zenorocha.github.io/clipboard.js
*
* Licensed MIT © Zeno Rocha
*/
<script>Zepto(function($)
var $leftPanel = $('.left-panel');
var $frameContainer = $('.frames-container');
var $appFramesTab = $('#application-frames-tab');
var $allFramesTab = $('#all-frames-tab');
var $container = $('.details-container');
var $activeLine = $frameContainer.find('.frame.active');
var $activeFrame = $container.find('.frame-code.active');
var $ajaxEditors = $('.editor-link[data-ajax]');
var $header = $('header');
$header.on('mouseenter', function ()
if ($header.find('.exception').height() >= 145)
$header.addClass('header-expand');
);
$header.on('mouseleave', function ()
$header.removeClass('header-expand');
);
/*
* add prettyprint classes to our current active codeblock
* run prettyPrint() to highlight the active code
* scroll to the line when prettyprint is done
* highlight the current line
*/
var renderCurrentCodeblock = function(id)
// remove previous codeblocks so we only render the active one
$('.code-block').removeClass('prettyprint');
// pass the id in when we can for speed
if (typeof(id) === 'undefined' || typeof(id) === 'object')
var id = /frame\-line\-([\d]*)/.exec($activeLine.attr('id'))[1];
$('#frame-code-linenums-' + id).addClass('prettyprint');
$('#frame-code-args-' + id).addClass('prettyprint');
prettyPrint(highlightCurrentLine);
/*
* Highlight the active and neighboring lines for the current frame
* Adjust the offset to make sure that line is veritcally centered
*/
var highlightCurrentLine = function()
var activeLineNumber = +($activeLine.find('.frame-line').text());
var $lines = $activeFrame.find('.linenums li');
var firstLine = +($lines.first().val());
// We show more code than needed, purely for proper syntax highlighting
// Let’s hide a big chunk of that code and then scroll the remaining block
$activeFrame.find('.code-block').first().css(
maxHeight: 345,
overflow: 'hidden',
);
var $offset = $($lines[activeLineNumber - firstLine - 10]);
if ($offset.length > 0)
$offset[0].scrollIntoView();
$($lines[activeLineNumber - firstLine - 1]).addClass('current');
$($lines[activeLineNumber - firstLine]).addClass('current active');
$($lines[activeLineNumber - firstLine + 1]).addClass('current');
$container.scrollTop(0);
/*
* click handler for loading codeblocks
*/
$frameContainer.on('click', '.frame', function()
var $this = $(this);
var id = /frame\-line\-([\d]*)/.exec($this.attr('id'))[1];
var $codeFrame = $('#frame-code-' + id);
if ($codeFrame)
$activeLine.removeClass('active');
$activeFrame.removeClass('active');
$this.addClass('active');
$codeFrame.addClass('active');
$activeLine = $this;
$activeFrame = $codeFrame;
renderCurrentCodeblock(id);
);
var clipboard = new Clipboard('.clipboard');
var showTooltip = function(elem, msg)
elem.setAttribute('class', 'clipboard tooltipped tooltipped-s');
elem.setAttribute('aria-label', msg);
;
clipboard.on('success', function(e)
e.clearSelection();
showTooltip(e.trigger, 'Copied!');
);
clipboard.on('error', function(e)
showTooltip(e.trigger, fallbackMessage(e.action));
);
var btn = document.querySelector('.clipboard');
btn.addEventListener('mouseleave', function(e)
e.currentTarget.setAttribute('class', 'clipboard');
e.currentTarget.removeAttribute('aria-label');
);
function fallbackMessage(action)
var actionMsg = '';
var actionKey = (action === 'cut' ? 'X' : 'C');
if (/Mac/i.test(navigator.userAgent))
actionMsg = 'Press ⌘-' + actionKey + ' to ' + action;
else
actionMsg = 'Press Ctrl-' + actionKey + ' to ' + action;
return actionMsg;
function scrollIntoView($node, $parent)
var nodeOffset = $node.offset();
var nodeTop = nodeOffset.top;
var nodeBottom = nodeTop + nodeOffset.height;
var parentScrollTop = $parent.scrollTop();
var parentHeight = $parent.height();
if (nodeTop < 0)
$parent.scrollTop(parentScrollTop + nodeTop);
else if (nodeBottom > parentHeight)
$parent.scrollTop(parentScrollTop + nodeBottom - parentHeight);
$(document).on('keydown', function(e)
var applicationFrames = $frameContainer.hasClass('frames-container-application'),
frameClass = applicationFrames ? '.frame.frame-application' : '.frame';
if(e.ctrlKey || e.which === 74 || e.which === 75)
// CTRL+Arrow-UP/k and Arrow-Down/j support:
// 1) select the next/prev element
// 2) make sure the newly selected element is within the view-scope
// 3) focus the (right) container, so arrow-up/down (without ctrl) scroll the details
if (e.which === 38 /* arrow up */ || e.which === 75 /* k */)
$activeLine.prev(frameClass).click();
scrollIntoView($activeLine, $leftPanel);
$container.focus();
e.preventDefault();
else if (e.which === 40 /* arrow down */ || e.which === 74 /* j */)
$activeLine.next(frameClass).click();
scrollIntoView($activeLine, $leftPanel);
$container.focus();
e.preventDefault();
else if (e.which == 78 /* n */)
if ($appFramesTab.length)
setActiveFramesTab($('.frames-tab:not(.frames-tab-active)'));
);
// Render late enough for highlightCurrentLine to be ready
renderCurrentCodeblock();
// Avoid to quit the page with some protocol (e.g. IntelliJ Platform REST API)
$ajaxEditors.on('click', function(e)
e.preventDefault();
$.get(this.href);
);
// Symfony VarDumper: Close the by default expanded objects
$('.sf-dump-expanded')
.removeClass('sf-dump-expanded')
.addClass('sf-dump-compact');
$('.sf-dump-toggle span').html('▶');
// Make the given frames-tab active
function setActiveFramesTab($tab)
$tab.addClass('frames-tab-active');
if ($tab.attr('id') == 'application-frames-tab')
$frameContainer.addClass('frames-container-application');
$allFramesTab.removeClass('frames-tab-active');
else
$frameContainer.removeClass('frames-container-application');
$appFramesTab.removeClass('frames-tab-active');
$('a.frames-tab').on('click', function(e)
e.preventDefault();
setActiveFramesTab($(this));
);
);
</script>
</body>
</html>
当我传递 orderId 时,我希望在回显 orderId 时看到“12345”。
谁能解释我做错了什么?
编辑: 当我运行以下命令并且我没有在 createReturn 中解码 JSON 时,它会按预期在控制台中打印“hallo”:
curl --header "Content-Type: application/json" \
--data '"orderId":12345,"customerNr":98765' \
https://returns.jdoe.blah.test/createReturn
但是,当我运行上面的 curl 并尝试在 createReturn 中解码 JSON 时,它会打印上面提到的整个 html 文件。所以它似乎与JSON解码有关,也许?
【问题讨论】:
可能是个愚蠢的问题,但您是否在请求周期的早期检查过您不是dump
-ing 您的.env
?我看到数据有 sf-dump
类等,据我所知,这来自使用 symfony dump
命令,而不是 print_r
。简而言之,您是否真的看到脚本打印了您的echo "hallo\n";
行?
@Kyrre 我如何知道我是否在请求周期的早期转储了 .env 文件?这听起来很愚蠢,但据我所知,我正在调用 uri /createReturn,它进入我的控制器 ProductReturnsController,然后调用函数 createReturn,它应该做的就是回显“你好”。
@Kyrre 另外,我刚刚删除了 json 对象并运行: curl -X POST returns.jdoe.blah.test/createReturn 它再次给了我整个 html 文件。但是昨天当我在没有 JSON 对象的情况下运行命令时,它工作并打印了“hello”。我不知道它为什么会改变。
我会在整个项目范围内搜索dump
函数,然后从ìndex.php
(或app/app_dev.php
,取决于您的Symfony 版本)开始处理请求。除此之外,很抱歉我不能提供更多帮助..不知道curl的来龙去脉>_>
@Kyrre 谢谢:)。我现在将继续进行项目范围的搜索。顺便说一句,我刚刚尝试了 curl returns.jdoe.blah.test/createReturn 没有 GET 或 POST 并且它按预期打印了“你好”。我不知道为什么 POST 不起作用。
【参考方案1】:
我发现我做错了什么:)。
我将卷曲改为:
curl -i -H "Accept: application/json" \
--data '"orderId":"12345", "customerNr":"98765"' \
-H "Content-Type: application/json" \
-X POST https://returns.jdoe.blah.test/createReturn
我在 ProductReturnsController.php 中将我的方法编辑为:
public function createReturn(Request $request)
$arr = $request->all();
foreach ($arr as $value)
echo "Current value is: " . $value . "\n";
我的问题是我只是使用 $json 作为参数,但我需要使用 Request $request 对参数进行类型提示,然后访问 $request 以获取存储在 JSON 对象中的信息。
例如,如果我只想访问 orderId,就像我在上面的问题中尝试的那样,我可以使用 input() 而不是 all(): p>
public function createReturn(Request $request)
$orderId = $request->input('orderId');
echo "The order id is: " . $orderId . "\n";
希望这可以帮助其他人,如果他们遇到同样的问题。
【讨论】:
以上是关于在命令行中使用 curl 通过 JSON 对象发布数据的主要内容,如果未能解决你的问题,请参考以下文章
Curl 和 PHP - 如何通过 curl 通过 PUT、POST、GET 传递 json