我正在尝试使用rest模板从Apple inapp购买api进行验证,但失败。 (在邮递员中工作正常)。邮递员集合:collection postman
如何使用rest模板对此进行归档?是否不允许base64加密数据?
HttpHeaders httpHeaders =新的HttpHeaders();
// httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add(Constant.PURCHASE.RECEIPT_DATA, purchase.getReceiptData());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, httpHeaders);
ResponseEntity<String> postResponse = restTemplate.postForEntity(iosPurchaseService, request, String.class);`
最佳答案
您可以使用具有以下属性的对象来发送需要提供的值作为API调用的输入,而不是在MultiValueMap中提供它。
public class SomeObject implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("receipt-data")
private String receiptdata;
}
然后按如下所示将此对象绑定到控制器中。
public void apiCall(@RequestBody SomeObject someObject) {
//Method 1
ResponseEntity<String> response1 = restTemplate.postForEntity("https://sandbox.itunes.apple.com/verifyReceipt", someObject,
String.class);
// or Method 2
ResponseEntity<String> response2 = restTemplate.exchange("https://sandbox.itunes.apple.com/verifyReceipt", HttpMethod.POST, new HttpEntity<>(someObject, null),
String.class);
}