2017-05-24 436 views
1

我试试这样的邮差:如何从guzzle使用auth和body raw获得响应?

我只填写输入密码。然后我点击按钮更新请求

这样的视图:

enter image description here

这是标头:

enter image description here

这是身体。我选择原始输入数据是这样的:

enter image description here

然后我点击按钮来发送,它可以得到的回应

但是当我尝试使用狂饮6这样的:

public function testApi() 
{ 
    $requestContent = [ 
     'headers' => [ 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json' 
     ], 
     'json' => [ 
      'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'], 
      'ids' => ["1"] 
     ] 
    ]; 
    try { 
     $client = new GuzzleHttpClient(); 
     $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent); 
     $response = json_decode($apiRequest->getBody()); 
     dd($response); 
    } catch (RequestException $re) { 
      // For handling exception. 
    } 
} 

结果为空

我该如何解决这个问题?

回答

1

请参阅Postman,您在“标题”选项卡中正确指定了字段Authorization。所以当你使用Guzzle时,应该是一样的,把它放在标题中:

public function testApi() 
{ 
    $requestContent = [ 
     'auth' => ['username', 'password'] 
     'headers' => [ 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json', 
     ], 
     'json' => [ 
      'ids' => ["1"] 
     ] 
    ]; 

    try { 
     $client = new GuzzleHttpClient; 
     $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent); 
     $response = json_decode($apiRequest->getBody()); 
     dd($response); 
    } catch (RequestException $re) { 
      // For handling exception. 
    } 
} 
+0

看来我的服务器有问题了。所以我不能尝试它。稍后当我尝试它,它的工作。我会接受你的回答 –

+0

好的,让我知道。我编辑了我的答案,因为我忘记了'Content-Type'=>'application/json'这行末尾的逗号。 – louisfischer

+0

我试过了。但它不起作用。它重定向到页面登录。它似乎不太认证。我在你的编码中看到,没有认证。但在我展示的邮差中,这是验证码 –