Rest模板无法发送Apple验证数据

时间:2018-05-28 15:14:49

标签: ios spring spring-boot resttemplate

我正在尝试使用rest模板从apple inapp purchase api进行验证,但它失败了。 (邮递员工作正常)。邮差收集:collection postman

如何使用rest模板将其归档?是不允许使用base64的数据?

`HttpHeaders httpHeaders = new 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);`

1 个答案:

答案 0 :(得分:1)

您可以使用具有以下属性的对象来发送您需要提供的值作为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);

}