所以,我正在尝试使用 API(使用 spring)做一个 cURL 代码 POST。问题是我尝试了 3 种不同的方法,但都没有奏效。我收到 412 错误。
这是卷曲代码:
curl -i -H "Accept: application/json" -H "Content-Type: application/json"
-X POST -u user:password -d '{ "external_id":"PES001", "name":"João da Silva", "email":"joao@empresa.com.br", "login":"joao@empresa.com.br", "password":"123456", "phone _number":"555199999999" }' https://xxxxxxxxx
这是我的java代码:
HttpHeaders createHeaders( )
{
return new HttpHeaders()
{
{
String auth = "xxxxxxxxx@xxxxxxxxx" + ":" + "xxxxxxxxx";
byte[] encodedAuth = Base64.encode(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );
set( "Authorization", authHeader );
}
};
}
@RequestMapping(value = "/createuser", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> sendCreateUser()
{
RestTemplate restTemplate = new RestTemplate();
// HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
// DefaultHttpClient httpClient =
// (DefaultHttpClient) requestFactory.getHttpClient();
// httpClient.getCredentialsProvider().setCredentials(
// new AuthScope("https://xxxxxxxxx",443, AuthScope.ANY_REALM),
// new UsernamePasswordCredentials("xxxxx", "xxxxxx"));
HttpHeaders httpHeaders = createHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(acceptableMediaTypes);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
// restTemplate.setMessageConverters(messageConverters);
// handler para controlar as requisições
// restTemplate.setErrorHandler(new UmovErrorHandler());
List<String> criarValor = new ArrayList<String>();
criarValor.add("external_id : TST5");
criarValor.add("name : Teste5");
criarValor.add("email : teste5@empresa.com.br");
criarValor.add("login : teste5@empresa.com.br");
criarValor.add("password : 123456");
criarValor.add("phone_number : 555199999995");
MultiValueMap<String, String> values = new LinkedMultiValueMap<String, String>();
values.put("data", criarValor);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
String outputBytes = "{'external_id': 'TST5644235', 'name': 'Test5443435e5', 'email': 't5454este43435@empresa.com.br', 'login': 'teste54545@empresa.com.br', 'password': '1234545456', 'phone_number': '555545199999995'}";
map.add("data", outputBytes);
// map.add("external_id","TST5");
// map.add("name","Teste5");
// map.add("email","teste5@empresa.com.br");
// map.add("login","teste5@empresa.com.br");
// map.add("password","123456");
// map.add("phone_number","555199999995");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, httpHeaders);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJacksonHttpMessageConverter teste = new MappingJacksonHttpMessageConverter();
messageConverters.add(new MappingJacksonHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
String teste22 = restTemplate.postForObject("https://xxxxxxxxx", request, String.class);
String test32 = null;
// integrationService.putSendPlanejamento(osOperacao);
return null;
}
如您所见,此代码中列出了我尝试过的所有 3 种方法。有人可以帮帮我吗 ?