Liadz
Liadz

Reputation: 51

How to authenticate POST requests to a RESTfull application using OAuth-Signpost and Apache HttpComponents?

I'm using OAuth-Signpost 1.2 to authenticate requests to a Magento RESTful application.

GET requests were pretty easy to implement using simply HttpURLConnection, but, so far as I understand, for POST requests I'd have to also use a library like Apache HttpComponents HttpClient. However, I'm not being able to figure out the right way to do so.

My current code is as follows:

public static void try2(OAuthConsumer consumer , String API_URL, String postInput) throws Exception{
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(API_URL);

    try {
        StringEntity input = new StringEntity(postInput);
        input.setContentType("application/json");
        postRequest.setEntity(input);
        consumer.sign(postRequest);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 201) {
            System.out.println("Failure. Code: " + response.getStatusLine().getStatusCode());
        }

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

But all I get is an error 500.

In a more general way my question would be: how can I sign POST requests to a RESTful app using signpost?

Upvotes: 2

Views: 479

Answers (0)

Related Questions