【问题标题】:"Server cannot understand Content-Type HTTP" error when retrieving a token检索令牌时出现“服务器无法理解 Content-Type HTTP”错误
【发布时间】:2016-04-29 15:38:29
【问题描述】:
$adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token';

$data = array("username" => "myname", "password" => "mypassword");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type=> application/json',                                                                                
    'Content-Length=> ' . strlen($data_string))                                                                       
);       
$token = curl_exec($ch);
print_r($token);

我尝试了上面的代码,但无法获取令牌,而是返回错误消息:

({"message":"服务器无法理解Content-Type HTTP头媒体类型application/x-www-form-urlencoded"},

我该如何解决这个错误?

【问题讨论】:

    标签: rest magento2


    【解决方案1】:
    //Authentication rest API magento2.Please change url accordingly your url
    $adminUrl='http://127.0.0.1/magento2/index.php/rest/V1/integration/admin/token';
    $ch = curl_init();
    $data = array("username" => "wsuser", "password" => "password123");                                                       
    
    $data_string = json_encode($data);                       
    $ch = curl_init($adminUrl); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );       
    $token = curl_exec($ch);
    $token=  json_decode($token);
    

    来自https://blog.i13websolution.com/magento-2-rest-api-example/

    【讨论】:

      【解决方案2】:

      你应该试试这个:

      curl -X POST "https://magento.host/index.php/rest/V1/integration/customer/token" \
           -H "Content-Type:application/json" \
           -d '{"username":"user_example", "password":"123123q"}'
      

      更多详情http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

      【讨论】:

        【解决方案3】:

        以下代码对我有用。

        //Magento admin user data
        $userData = array("username" => "adminuser", "password" => "adminpassword");
        $ch = curl_init("http://yourdomain.com/rest/V1/integration/admin/token");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData))));
        
        $token = curl_exec($ch);
        

        参考:http://inchoo.net/magento-2/magento-2-api/

        【讨论】:

        • 解释您的代码并提供更多细节。会有帮助的。
        • 欢迎来到 Stack Overflow!尽管此代码可能有助于解决问题,但它并没有解释 why 和/或 如何 回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的回答添加解释,包括适用的限制和假设。
        【解决方案4】:

        请更改您的 $adminUrl

        来自:

        http://localhost/magento/index.php/rest/V1/integration/admin/token
        

        到:

        http://localhost/rest/V1/integration/admin/token
        

        这样就好了。

        【讨论】:

          【解决方案5】:

          您可以使用它来获取管理员令牌。

          $curl = curl_init();
          
          curl_setopt_array($curl, array(
            CURLOPT_URL => "http://127.0.0.1/www.yourstoreurl.com/rest/V1/integration/admin/token",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "{\r\n  \"username\": \"admin\",\r\n  \"password\": \"pwd@123\"\r\n}",
            CURLOPT_HTTPHEADER => array(
              "cache-control: no-cache",
              "content-type: application/json",
              "postman-token: e3deae08-0436-af2d-0449-450720bb7086"
            ),
          ));
          
          $response = curl_exec($curl);
          $err = curl_error($curl);
          
          curl_close($curl);
          
          if ($err) {
            echo "cURL Error #:" . $err;
          } else {
            echo $response;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-28
            • 2021-12-12
            • 2020-07-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多