1

This is the URL

curl -H "Authorization: Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L" https://api.url.com/v1/market/total-items.json

I want to use it on JAVA where oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L value will be a dynamic value which i receive using a variable.

3
  • 1
    Curl is a bash command, you need to implement a GET and/or POST to do your request. You can use a library or do it your self. See Retrofit or Volley, there are good library. Commented Apr 22, 2016 at 9:33
  • add "Authorization: Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L" to your header. Key is Authorization and value is Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L Commented Apr 22, 2016 at 9:48
  • Could you provide me sample php code for that? Commented Apr 26, 2016 at 4:50

1 Answer 1

0

This is how you do it in Java. curl - h is taken care by httpPost.addHeader() curl - d by the form body curl -u at least in the case of this API (e.g stripe) it was the authorization Bearer. In case of Oauth2, curl -u is passing username:password in the header e.g

form.add(new BasicNameValuePair("username", request.getParameter("username")));

CloseableHttpClient userWebTokenHTTPclient;
        if (HIFIConstants.FULL_URL_HIFINITE.contains("localhost")) {  //for localhost HTTP request fails due to certificate issues, hence workaround. Works for Beta etc without SSLContext

            SSLContextBuilder sslcontext = new SSLContextBuilder();
            sslcontext.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            userWebTokenHTTPclient = HttpClients.custom().setSSLContext(sslcontext.build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .build();
        } else
            userWebTokenHTTPclient = HttpClients.createDefault();



        HttpPost httpPost = new HttpPost("https://api.somepublicsite.com/v1/ident/verits");

        List<NameValuePair> form = new ArrayList<>();
        form.add(new BasicNameValuePair("return_url", "https://example-dev.example.com?vi={VERIFICATION_INTENT_ID}"));
        form.add(new BasicNameValuePair("requested_verifications[0]", "identity_document"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
        httpPost.setEntity(entity);

        httpPost.addHeader("Authorization", "Bearer somesecretkeyhere"); 
        httpPost.addHeader("Version", "v3");
        httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");    
        CloseableHttpResponse tokenResponse = userWebTokenHTTPclient.execute(httpPost);
        org.json.JSONObject verificationIntent = new org.json.JSONObject(EntityUtils.toString(tokenResponse.getEntity()));
        userWebTokenHTTPclient.close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.