0

I am trying to POST some data to rest api, When I send the request to API using SPRING REST I get the 403 exception.

I have tried adding user-agent header as suggested by other answers but nothing has worked for me so far. I also checked that access key when using POSTMAN and when calling the service is same. Any advice would be helpful;

The wrapper class to create the body of POST request

public class ApiRequest implements Serializable {

private static final long serialVersionUID = 3729607216939594972L;
@JsonProperty("id")
List<Integer> id;
@JsonProperty("sdate")
String sdate;
@JsonProperty("edate")
String edate;
@JsonProperty("fields")
List<String> fields;
public ApiRequest(List<Integer> id, String sdate, String edate, List<String> fields){
    this.id=id;
    this.sdate=sdate;
    this.edate=edate;
    this.fields=fields;
}

public void setEdate(String edate) {
    this.edate = edate;
}
public void setSdate(String sdate){
    this.sdate=sdate;
}

public void setFields(List<String> fields) {
    this.fields = fields;
}

public void setId(List<Integer> id) {
    this.id = id;
}

public String getEdate() {
    return edate;
}

public String getSdate() {
    return sdate;
}

public List<String> getFields() {
    return fields;
}

public List<Integer> getId() {
    return id;
}

@Override
public String toString() {
    return "ApiRequest{" +
            "id=" + id +
            ", sdate=" + sdate +
            ", edate=" + edate +
            ", fields=" + fields+
            '}';
}

}

Code to call the api

private HttpHeaders getRequestHeaders() {

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Arrays.asList(MediaType.ALL));
requestHeaders.set("user-agent","Some User Agent);
requestHeaders.set("access_token", "ACCESS_TOKEN");
        return requestHeaders;
}
ApiRequest request=new    ApiRequest(Arrays.asList(10),DateUtil.today().toString(),DateUtil.today().plusDays(10).toString(),Arrays.asList("ALL"));

String response=post("RANDOM_URL",null,null,request,getRequestHeaders(),String.class,"");

Post super method:

public <T> T post(String baseUrl, String url, String query, Object body, HttpHeaders requestHeaders, Class<T> responseClassType, String logTag) {
// In this method body is converted to Json String and called the restExchange
  • 403 means that you're not allowed to access the resource. So either your access token is wrong, or it's not sent as the API expects it, or it gives you access to some resources, but not to the one you're requesting. Passing `null` as the URL doesn't look like a good idea to me. – JB Nizet Aug 25 '19 at 07:55
  • but same access token gives access to the resource – Inderjit Chopra Aug 25 '19 at 08:04

1 Answers1

0

If you are sure that with Postman you are getting correct results then you can enable debug logs for the underlying httpclient ( if apache http client is the underlying http library) by setting logging.level.org.apache.http=DEBUG. This will print all the request details like url, headers etc by which you can compare with what you are sending with Postman. If the client library is something different then you may need to write an interceptor to capture all the request details as explained here.

Shailendra
  • 8,874
  • 2
  • 28
  • 37