使用基本身份验证和发送json数据进行卷曲

时间:2018-06-12 13:38:59

标签: php authentication curl post php-curl

我在命令提示符下使用此代码来测试我是否可以获得将数据发送到给定网址的授权。

read_to_end

但我认为它无法接收POST方法数据,因为:

curl --header "Authorization":"base64" -H "Content-Type: text/html; charset=UTF-8" -H "Accept: application/json" -X POST -d '{"firstname":"test","lastname":"test","mobile_number":09999999999,"education":"Bachelor's Degree","email":"test@gmail.com","job_opening_id":123}' https://www.test.com -v

allow header只有GET和HEAD方法,如果我认为是正确的,它真的不接受我的数据。

但是当我只运行此代码时:

< HTTP/1.1 405 Method Not Allowed
< Server: nginx/1.13.12
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< allow: GET, HEAD
< Cache-Control: no-cache, private

它会正常运行但我无法以这种方式发送数据。所以我的问题是我做错了什么?或者如果它是导致问题的allow标头。谢谢伙伴们!

1 个答案:

答案 0 :(得分:0)

尝试:

curl -i --header "Authorization":"base64" "https://www.test.com" -G --data-urlencode json='{"firstname":"test","lastname":"test","mobile_number":09999999999,"education":"Bachelors Degree","email":"test@gmail.com","job_opening_id":123}'

返回:

HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Wed, 13 Jun 2018 01:23:42 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=20
X-DIS-Request-ID: d065c375da705938b3d3e136fc1dda23
P3P: CP="NON DSP COR ADMa OUR IND UNI COM NAV INT"
Cache-Control: no-cache

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getCookie(c_name) { // Local function for getting a cookie value
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
        c_start=c_start + c_name.length + 1;
        c_end=document.cookie.indexOf(";", c_start);

        if (c_end==-1)
            c_end = document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) { // Local function for setting a value of a cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getHostUri() {
    var loc = document.location;
    return loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '96.93.126.65', 10);
try {
    location.reload(true);
} catch (err1) {
    try {
        location.reload();
    } catch (err2) {
        location.href = getHostUri();
    }
}
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body>
</html>

-G标记使所有数据都在HTTP GET而不是POST中使用:

  

-G, - get

     

使用时,此选项将使用-d, - data, - data-binary或--data-urlencode指定的所有数据在HTTP GET请求中使用,而不是使用否则将使用的POST请求。数据将附加到带有'?'的URL分离器。

这篇文章可能会有所帮助:Curl GET with JSON parametr. Empty reply from server

相关问题