0

I am trying to send a simple POST request to Bonanza API for AddFixedPriceItem . They give a java example, but it is not working. This is the code page http://api.bonanza.com/docs/examples/java

import org.json.JSONObject;
import org.json.JSONStringer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;

class AddFixedPriceItem {
    public static void main(String[] args) {
        try {
            String devId = "t************I";
            String certId = "l***********F";

            URL url = new URL("https://api.bonanza.com/api_requests/secure_request");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
            connection.setRequestProperty("X-BONANZLE-API-CERT-NAME", certId);

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            String jsonPayload = new JSONStringer()
                .object()
                    .key("requesterCredentials")
                    .object()
                        .key("bonanzleAuthToken")
                        .value("myAuthToken")
                    .endObject()

                    .key("item")
                    .object()
                        .key("title")
                        .value("Lightsaber")

                        .key("primaryCategory")
                        .object()
                            .key("categoryId")
                            .value("163128")
                        .endObject()

                        .key("description")
                        .value("An elegant weapon, for a more civilized age<br>* SELLER <strong>NOT LIABLE</strong> FOR DISMEMBERMENT *")
                        .key("price")
                        .value("42")
                        .key("quantity")
                        .value("3")
                        .key("shippingType")
                        .value("Free")

                        .key("itemSpecifics")
                        .array()
                            .array()
                                .value("condition")
                                .value("used")
                            .endArray()
                            .array()
                                .value("danger")
                                .value("extreme")
                            .endArray()
                        .endArray()

                        .key("pictureDetails")
                        .object()
                            .key("pictureURL")
                            .array()
                                .value("http://images.discountstarwarscostumes.com/products/9284/1-1/luke-skywalker-blue-lightsaber.jpg")
                                .value("http://www.rankopedia.com/CandidatePix/29862.gif")
                            .endArray()
                        .endObject()

                        .key("variations")
                            .array()
                                .object()
                                    .key("quantity")
                                    .value("2")
                                    .key("nameValueList")
                                    .array()
                                        .object()
                                            .key("name")
                                            .value("Colour")
                                            .key("value")
                                            .value("Blue")
                                        .endObject()
                                        .object()
                                            .key("name")
                                            .value("Style")
                                            .key("value")
                                            .value("Single")
                                        .endObject()
                                    .endArray()
                                .endObject()
                                .object()
                                    .key("quantity")
                                    .value("1")
                                    .key("nameValueList")
                                    .array()
                                        .object()
                                            .key("name")
                                            .value("Colour")
                                            .key("value")
                                            .value("Red")
                                        .endObject()
                                        .object()
                                            .key("name")
                                            .value("Style")
                                            .key("value")
                                            .value("Double")
                                        .endObject()
                                    .endArray()
                                .endObject()
                            .endArray()
                    .endObject()
                .endObject()
                .toString();

            String requestName = "addFixedPriceItemRequest";

            String toWrite = requestName + "=" + jsonPayload;

            writer.write(toWrite);
            writer.flush();
            writer.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String response = in.readLine();

            JSONObject jsonResponse = new JSONObject(response);

            if (jsonResponse.optString("ack").equals("Success") 
                    && jsonResponse.optJSONObject("addFixedPriceItemResponse") != null) {

                // Success! Now read more keys from the json object
                JSONObject outputFields = jsonResponse.optJSONObject("addFixedPriceItemResponse");
                System.out.println("Item ID: " + outputFields.optInt("itemId"));
                System.out.println("Category ID: " + outputFields.optInt("categoryId"));
                System.out.println("Selling State: " + outputFields.optString("sellingState"));
            } else {
                System.out.println(jsonResponse);
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

getting error

java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.bonanza.com/api_requests/secure_request

if more info is needed please let me know

Thank you for your future help

Simbu
  • 682
  • 1
  • 9
  • 29

1 Answers1

0

Status Code 500 is an Internal Server Error. 500 indicates that a part of the server (e.g. a CGI program) has crashed or may be encountered a configuration error.

For this error you can read getErrorStream() of HttpURLConnection. It will give related information about the problem.

For better understanding you can follow related links:

  1. Using java.net.URLConnection to fire and handle HTTP requests
  2. java.io.IOException: Server returned HTTP response code: 500
Community
  • 1
  • 1
SkyWalker
  • 24,796
  • 7
  • 62
  • 118
  • After using getErrorStream(), we received the JSON error message. `{"ack":"Failure","version":"1.0beta","timestamp":"2016-03-13T03:03:51.000Z","errorMessage":{"message":"Cannot determine what type of request you are making. Often this can be the result of data that has not been escaped before being passed to the API. If you are passing data with quotation marks or other special characters, you should translate it to JSON, then escape it, before sending it over the API."}}` This does not give any info which will be used to correct mistake. – Simbu Mar 13 '16 at 10:04