在HttpClient发布请求中包含标头和正文
使用HttpClient在Angular 6中发布带有正文和标头的请求 并且它使用摘要验证不起作用
标题是(与邮递员使用的相同)
Content-Type:应用程序/ x-www-form-urlencoded
授权:摘要用户名=“ admin”密码=“ 123456”,realm =“ Web服务API”,nonce =“”,uri =“ / my / url”,qop = auth,nc =,cnonce =“” ,response =“ 2bb656ae5cd23accefc542f99e1b427f”,opaque =“”
身体是
default_device:默认
我的角度代码为
getData(){
let headers = new HttpHeaders();
headers = headers
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Authorization','Digest '
+ ('username=admin')
+ ('password=123456')
+ ('realm=Web Service API')
+ ('nonce=5b51bf3a911d8')
+ ('uri=/my/url')
+ ('qop=auth')
+ ('nc=00000009')
+ ('cnonce=a6f5acf57475e6a6')
+ ('response=25c3438312279b190da5593cd658aaaf')
+ ('opaque=ad85287bbf9e700d6817070cd3216cdf')
+ ('default_device=default'));
this.httpClient.post(this.authurl,{default_device:'default'},{headers:headers})
.subscribe(data=>{console.log("data",data);
})
}
在控制台中出现错误消息 加载资源失败:服务器响应状态为401(未经授权)
疑问:
我应该在哪里输入用户名和密码?(在正文或标题中)
在邮递员中,我得到了一个成功的json数组。 我用php尝试了同样的方法,效果很好。
相同的PHP代码
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$url = "www.domain.com/url";
$username = "admin";
$password = "123456";
$post_data = array(
'default_device' => 'default',
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false, // for https
CURLOPT_USERPWD => $username . ":" . $password,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post_data)
);
$ch = curl_init();
curl_setopt_array( $ch, $options );
try {
$raw_response = curl_exec( $ch );
// validate CURL status
if(curl_errno($ch))
throw new Exception(curl_error($ch), 500);
// validate HTTP status code (user/password credential issues)
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status_code != 200)
throw new Exception("Response with Status Code [" . $status_code . "].", 500);
} catch(Exception $ex) {
if ($ch != null) curl_close($ch);
throw new Exception($ex);
}
if ($ch != null) curl_close($ch);
echo "raw response: " . $raw_response;
?>
响应是json数组。
请帮助我了解角度6中的标头和正文的语法