gpt4 book ai didi

java - 如何在假装客户端中传递 header ,尤其是 HTTP header ?

转载 作者:行者123 更新时间:2023-12-05 08:07:39 25 4
gpt4 key购买 nike

我尝试使用 Rest Template 传递 http header 并按预期工作,但有些它无法与假客户端一起工作。

休息客户端代码:-

import org.springframework.http.HttpHeaders;

RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeader = new HttpHeaders();
httpHeader.set("appsecret-proof", header);
HttpEntity<?> request = new HttpEntity<>(httpHeader);
String url = "https://localhost/groups/{pathVariable}/members/";
Map<String, String> map = new HashMap<>();
map.put("pathVariable", pathVariable);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("token", token);
String uriBuilder = builder.build().toString();

ResponseEntity<MemberObject> responseEntity = restTemplate.exchange(uriBuilder, HttpMethod.POST, request,MemberObject.class, map);

MemberObject memberObject = null;
if (responseEntity != null && responseEntity.getBody() != null) {
memberObject = responseEntity.getBody();
}

Fegin 客户端代码:-

import org.springframework.web.bind.annotation.RequestHeader;

@PostMapping(value = "/groups/?token=xyz")
MemberObject getMemberGroup(@RequestHeader("appsecret-proof") String appsecretProof);

但是 fegin 客户端给出了一个错误:

{"error":{"message":"Authentication credentials could not be found.","type":"Authentication Error","code":404,"sub_code":0}}.

在 feign 客户端中硬编码 token 仍然给出相同的错误。 header 可能有误....关于如何在假客户端中设置 header 的任何建议。

最佳答案

如果你使用 Feign,你应该如何构建调用:

import feign.Headers;
import feign.Param;
import feign.RequestLine;
...
@RequestLine("POST /groups/?token={appSecretProof}")
@Headers("Content-Type: application/json")
MemberObject getMemberGroup(@Param("appSecretProof") String appSecretProof);

在上面的代码片段中,appSecretProof 的值在执行时被替换到 URL 中作为 token 的相应值。

关于java - 如何在假装客户端中传递 header ,尤其是 HTTP header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54306395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com