recently I've faced the problem of different behavior whenever I make a call through the Postman and programmatically with the same config. During call from code I receive 400 bad request and my debugging showed no explicit issues.
Here is I'm doing from postman: I make a call to https://api.cloud.wowza.com/api/v1.3/live_streams/{streamId}/start with presseted headers wsc-api-key and wsc-access-key and method type PUT and everything is going smoothly.
What I'm doing in code :
I've set up a HttpEntity with headers and json content type
@Bean
public HttpEntity<String> httpEntity() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("wsc-api-key", wscApiKey);
headers.add("wsc-access-key", wscAccessKey);
return new HttpEntity<>("parameters", headers);
}
created a simple method with PUT http method type
public Optional<LiveStreamWrapper> startStream(String streamId) {
String wowzaCloudStreamEndpoint = wowzaCloudUrl + "/" + streamId + "/start";
logger.info("Wowza endpoint :" + wowzaCloudStreamEndpoint);
ResponseEntity<LiveStreamWrapper> liveStreamWrapperResponseEntity = restTemplate.exchange(wowzaCloudStreamEndpoint, HttpMethod.PUT, entity, LiveStreamWrapper.class);
return Optional.ofNullable(liveStreamWrapperResponseEntity.getBody());
}
And here I'm receiving 400 bad request even with everything being the same. I'm cracking y head over this for quite some time, can somebody please give a hint?
UPD: postman request
{
"name": "stream start",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
},
{
"key": "wsc-api-key",
"value": "{{wsc-api-key}}",
"type": "text"
},
{
"key": "wsc-access-key",
"value": "{{wsc-access-key}}",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://api.cloud.wowza.com/api/v1.3/live_streams/pt4v2pf1/start",
"protocol": "https",
"host": [
"api",
"cloud",
"wowza",
"com"
],
"path": [
"api",
"v1.3",
"live_streams",
"pt4v2pf1",
"start"
]
}
},
"response": []
},
UPD: after I've followed the @Isank advice behaviour changed to HttpClientErrorException$UnprocessableEntity: 422 Unprocessable Entity
Now HttpEntity config looks like this
@Bean
public HttpEntity<String> httpEntity() {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("wsc-api-key", wscApiKey);
headers.set("wsc-access-key", wscAccessKey);
return new HttpEntity<>(headers);
}
UPD: I've resolved all my issues thank you everyone! In order to solve Unprocessable entity I've followed suggestions from that tread: RestTemplate--> Exchange produces: 422 Unprocessable Entity Happy coding!