0
Unirest.setTimeouts(7000, 7000);
         HttpResponse<JsonNode> accessToken = Unirest.post("https://api4.truecaller.com/v1/apps/requests")
                    .header("Content-Type", "application/json")
                    .header("Accept", "application/json")
                    .header("appKey", "app-key-here")
                    .header("Cache-Control", "no-cache")
                    .field("phoneNumber", "46760123456")
                    .asJson();

I am using TrueCaller Web-login API (https://github.com/truecaller/web-login),

The above mentioned is my code (Java-Unirest), please help me to solve this problem (Unable to call API)

Response from truecaller server : Error: 400 Message: Unable to process JSON.

  • Can you debug it and get the json that the object Unirest is building inside? also, I do not see your body at the moment, maybe there is the problem? – Federico José Sorenson Jun 08 '17 at 09:03
  • Console logs : XMLHttpRequest cannot load https://api4.truecaller.com/v1/apps/requests. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. From AngularJS code. – HARIOM VASHISTH Jun 08 '17 at 11:03
  • Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"https://api4.truecaller.com/v1/apps/requests","data":{"phoneNumber":919999421928},"headers":{"appKey":"xxx-xxx-xxx","Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":""} – HARIOM VASHISTH Jun 08 '17 at 11:06
  • but when i hit curl request directly from my server terminal , i get {"requestId":"xxx"} ??? – HARIOM VASHISTH Jun 08 '17 at 11:08
  • 1
    there is an error in that JSON, if you look closely, after "data" there is a semicolon followed by a colon. You can use tools like http://www.jsoneditoronline.org/ to validate your json, but I would believe that is the root cause of your problem – Federico José Sorenson Jun 08 '17 at 11:15

1 Answers1

1

http://unirest.io/java.html

You have to use body method as per the documentation.

Example

Unirest.setTimeouts(7000, 7000);
HttpResponse<JsonNode> accessToken = Unirest.post("https://api4.truecaller.com/v1/apps/requests")
 .header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("appKey", "app-key-here")
.header("Cache-Control", "no-cache")
.body("{\"phoneNumber\":7777777777}")
.asJson();

OR

Unirest.setObjectMapper(new ObjectMapper() {
    private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
    public <T> T readValue(String value, Class<T> valueType) {
                try {
                    return jacksonObjectMapper.readValue(value, valueType);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            public String writeValue(Object value) {
                try {
                    return jacksonObjectMapper.writeValueAsString(value);
                } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        });

class Test implements Serializable {
    final public Long phoneNumber;

    public Test(Long phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

Test t = new Test(7777777777L);

Unirest.setTimeouts(7000, 7000);

HttpResponse<JsonNode> accessToken = Unirest.post("https://api4.truecaller.com/v1/apps/requests")
    .header("Content-Type", "application/json")
    .header("Accept", "application/json")
    .header("appKey", "app-key-here")
    .header("Cache-Control", "no-cache")
    .body(t)
    .asJson();