How to send post data via JSON using Java

  • cartamoneta-com

    36 mensajes

    Italia

    Hello,

    i'm trying to use itemPost REST API to add new product using Java.
    I'm able to authenticate and do all GET requests but I'm in trouble with all POST and PUT requests. It is like no REST body was sent.

    Does anyone can provide a working java code for POST requests?

    This is my current approach (I also tried HttpURLConnection with same results):

    RestItem i = new RestItem();
    i.setId_category(21384);
    i.setFixed_price((float) 500);
    i.setCurrency("EUR");
    i.setTitle("PRODOTTO TEST - ARIA - DO NOT BUY");
    i.setPersonal_reference("doNotBuy");

    ArrayList imgLinks = new ArrayList();
    imgLinks.add(" Vinculo (https) ;
    i.setListaImmagini(imgLinks);

    String url = " Vinculo (https) ";

    Map dataMap = restItemToMap(i);

    JSONObject type = new JSONObject().put("type","fixedPrice");

    JSONArray itemDetailsA = new JSONArray();
    itemDetailsA.put(dataMap);

    JSONObject itemDetails = new JSONObject().put("item", itemDetailsA );

    JSONArray postData= new JSONArray().put(type).put(itemDetails);

    String payload = postData.toString();

    HttpClient httpclient = HttpClientBuilder.create().build();

    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-type", "application/json");

    StringEntity jsonPayload = new StringEntity(payload);
    jsonPayload.setContentEncoding("UTF-8");
    jsonPayload.setContentType("application/json");

    httppost.setEntity(jsonPayload);

    HttpResponse httpResponse = httpclient.execute(httppost);


    The response (Delcampe_Notification) :
    Ressource: item
    Action: POST
    Status: 400
    Error: err_item_incorrectIdCategory, err_item_emptyTitle, err_item_personalReferenceNeeded, Your parameter 'type' must be 'auction' or 'fixedPrice'!, err_item_incorrectPriceStarting, err_item_imageRequired, err_item_incorrectCurrency

    Thanks and best regards
  • @thomas
    Administrador

    348 mensajes

    Bélgica

    Hello,

    i'm trying to use itemPost REST API to add new product using Java.
    I'm able to authenticate and do all GET requests but I'm in trouble with all POST and PUT requests. It is like no REST body was sent.

    Does anyone can provide a working java code for POST requests?

    This is my current approach (I also tried HttpURLConnection with same results):

    RestItem i = new RestItem();
    i.setId_category(21384);
    i.setFixed_price((float) 500);
    i.setCurrency("EUR");
    i.setTitle("PRODOTTO TEST - ARIA - DO NOT BUY");
    i.setPersonal_reference("doNotBuy");

    ArrayList imgLinks = new ArrayList();
    imgLinks.add(" Vinculo (https) ;
    i.setListaImmagini(imgLinks);

    String url = " Vinculo (https) ";

    Map dataMap = restItemToMap(i);

    JSONObject type = new JSONObject().put("type","fixedPrice");

    JSONArray itemDetailsA = new JSONArray();
    itemDetailsA.put(dataMap);

    JSONObject itemDetails = new JSONObject().put("item", itemDetailsA );

    JSONArray postData= new JSONArray().put(type).put(itemDetails);

    String payload = postData.toString();

    HttpClient httpclient = HttpClientBuilder.create().build();

    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-type", "application/json");

    StringEntity jsonPayload = new StringEntity(payload);
    jsonPayload.setContentEncoding("UTF-8");
    jsonPayload.setContentType("application/json");

    httppost.setEntity(jsonPayload);

    HttpResponse httpResponse = httpclient.execute(httppost);


    The response (Delcampe_Notification) :
    Ressource: item
    Action: POST
    Status: 400
    Error: err_item_incorrectIdCategory, err_item_emptyTitle, err_item_personalReferenceNeeded, Your parameter 'type' must be 'auction' or 'fixedPrice'!, err_item_incorrectPriceStarting, err_item_imageRequired, err_item_incorrectCurrency

    Thanks and best regards
    Hello cartamoneta-com,

    Using a library can help you simplify the process.

    For example, with uniRest: Vinculo (http)

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.post(" Vinculo (https)
    .header("Content-Type", "application/x-www-form-urlencoded")
    .field("type", "fixedPrice")
    .field("item[id_category]", "21384")
    .field("item[fixed_price]", "500.00")
    .field("item[currency]", "EUR")
    .field("item[title]", "yourTitle")
    .field("item[personal_reference]", "yourPersonalReference")
    .field("item[qty]", "1")
    .field("item[duration]", "28")
    .field("item[description]", "yourDescription")
    .field("item[renew]", "0")
    .field("item[option_lastminutebidding]", "")
    .field("item[option_privatebidding]", "")
    .field("item[option_subtitle]", "")
    .field("item[subtitle]", "")
    .field("item[option_boldtitle]", "")
    .field("item[option_highlight]", "")
    .field("item[option_coloredborder]", "")
    .field("item[option_toplisting]", "")
    .field("item[option_topcategory]", "")
    .field("item[option_topmain]", "")
    .field("item[option_keepoptionsonrenewal]", "")
    .field("item[prefered_end_day]", "mon")
    .field("item[prefered_end_hour]", "15:00")
    .field("item[images][0]", "yourImageUrl")
    .asString();

    Best Regards,
    --
    Thomas
    Delcampe Team
  • cartamoneta-com

    36 mensajes

    Italia

    It works!

    Thanks
  • cartamoneta-com

    36 mensajes

    Italia

    Facing new issue with http PUT method.

    This is the code i'm using:

    RestItem i = new RestItem();
    i.setTitle("UPDATED TITLE - DO NOT BUY");
    i.setFixed_price((float) 1600);

    Integer itemID = ::theID ;

    Map dataMap = new HashMap();
    dataMap.put("item[title]", i.getTitle());
    dataMap.put("item[fixed_price]", i.getFixed_price());

    Unirest.config()
    .socketTimeout(0)
    .connectTimeout(0)
    .concurrency(10, 5)
    .followRedirects(false)
    .enableCookieManagement(false);

    String apiUrl = " Vinculo (https) " + theSku + "?token=myToken";

    HttpResponse response = Unirest.put(apiUrl).header("Content-Type", "application/x-www-form-urlencoded").fields(dataMap).asString();


    The response Status is 200 so non problems on call. The issue maybe related to data payload.

    Do you have any advice?
    Thanks and best regards.
  • @thomas
    Administrador

    348 mensajes

    Bélgica

    Facing new issue with http PUT method.

    This is the code i'm using:

    RestItem i = new RestItem();
    i.setTitle("UPDATED TITLE - DO NOT BUY");
    i.setFixed_price((float) 1600);

    Integer itemID = ::theID ;

    Map dataMap = new HashMap();
    dataMap.put("item[title]", i.getTitle());
    dataMap.put("item[fixed_price]", i.getFixed_price());

    Unirest.config()
    .socketTimeout(0)
    .connectTimeout(0)
    .concurrency(10, 5)
    .followRedirects(false)
    .enableCookieManagement(false);

    String apiUrl = " Vinculo (https) " + theSku + "?token=myToken";

    HttpResponse response = Unirest.put(apiUrl).header("Content-Type", "application/x-www-form-urlencoded").fields(dataMap).asString();


    The response Status is 200 so non problems on call. The issue maybe related to data payload.

    Do you have any advice?
    Thanks and best regards.
    Hello cartamoneta,

    The link you use in the variable 'apiUrl' is not correct to update an existing item.

    You should use "PUT /item/:idOfYourItem" instead of "PUT /item/reference/:yourPersonalRef".
    One way to proceed is to retrieve the identifier with a first call by reference : "GET /item/reference/:yourPersonalRef" and then update your item with "PUT /item/:idOfYourItem".

    Best Regards,
    --
    Thomas
    Delcampe Team
  • cartamoneta-com

    36 mensajes

    Italia

    Sorry it was only a typo when i copy/past the code here

    My actual url is this:

    String apiUpdate = " Vinculo (https) " + "/item/" + itemID + "?token=::TOKEN::";
  • @thomas
    Administrador

    348 mensajes

    Bélgica

    Sorry it was only a typo when i copy/past the code here

    My actual url is this:

    String apiUpdate = " Vinculo (https) " + "/item/" + itemID + "?token=::TOKEN::";
    Can you give me the id or link of the concerned sale?
  • cartamoneta-com

    36 mensajes

    Italia

    Can you give me the id or link of the concerned sale?
    This is the link of the test item (#1111365128) that i'm trying to update:
    Vinculo (https)
  • @thomas
    Administrador

    348 mensajes

    Bélgica

    This is the link of the test item (#1111365128) that i'm trying to update:
    Vinculo (https)
    Hello cartamoneta,

    I tested everything on my side and it worked well. Be aware that changes can take time. Also, maybe try to change the UniRest configuration if it still doesn't work properly.

    Best Regards,
    --
    Thomas
    Delcampe Team
  • cartamoneta-com

    36 mensajes

    Italia

    Still not able to update item using Unires library (or any other method)...

    Can you provide some working example, please

    Tanks
  • @thomas
    Administrador

    348 mensajes

    Bélgica

    Still not able to update item using Unires library (or any other method)...

    Can you provide some working example, please

    Tanks
    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Vinculo (https)
    .header("Content-Type", "application/x-www-form-urlencoded")
    .field("item[title]", "test API PUT")
    .field("item[personal_reference]", "123456")
    .asString();

    This code should work. I have not personally tested it as we do not have a Java infrastructure.

    Best Regards,
    --
    Thomas
    Delcampe Team
  • cartamoneta-com

    36 mensajes

    Italia

    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Vinculo (https)
    .header("Content-Type", "application/x-www-form-urlencoded")
    .field("item[title]", "test API PUT")
    .field("item[personal_reference]", "123456")
    .asString();

    This code should work. I have not personally tested it as we do not have a Java infrastructure.

    Best Regards,
    --
    Thomas
    Delcampe Team
    Still no luck.

    The response is always "Response: Your request was taken into account!" but actually no update also after 30 minutes.
  • cartamoneta-com

    36 mensajes

    Italia

    Hi,

    I'm still unable tu update any item using API REST


    This is the DEBUG LOG generated calling the API and the XML response :

    Vinculo (https)



    Thanks and Best regards.

Únese a la comunidad de coleccionistas

Inscripción Identificarse