How to send post data via JSON using Java

  • cartamoneta-com

    36 Beiträge

    Italien

    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(" Link (https) ;
    i.setListaImmagini(imgLinks);

    String url = " Link (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
    Administrator

    348 Beiträge

    Belgien

    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(" Link (https) ;
    i.setListaImmagini(imgLinks);

    String url = " Link (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
    • Erstellt am 12.10.2020 um 09:59
    • #1198066
    Hello cartamoneta-com,

    Using a library can help you simplify the process.

    For example, with uniRest: Link (http)

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.post(" Link (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 Beiträge

    Italien

    It works!

    Thanks
  • cartamoneta-com

    36 Beiträge

    Italien

    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 = " Link (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
    Administrator

    348 Beiträge

    Belgien

    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 = " Link (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.
    • Erstellt am 19.10.2020 um 05:12
    • #1199893
    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 Beiträge

    Italien

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

    My actual url is this:

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

    348 Beiträge

    Belgien

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

    My actual url is this:

    String apiUpdate = " Link (https) " + "/item/" + itemID + "?token=::TOKEN::";
    • Erstellt am 19.10.2020 um 06:16
    • #1199941
    Can you give me the id or link of the concerned sale?
  • cartamoneta-com

    36 Beiträge

    Italien

    Can you give me the id or link of the concerned sale?
    • Erstellt am 19.10.2020 um 07:45
    • #1199968
    This is the link of the test item (#1111365128) that i'm trying to update:
    Link (https)
  • @thomas
    Administrator

    348 Beiträge

    Belgien

    This is the link of the test item (#1111365128) that i'm trying to update:
    Link (https)
    • Erstellt am 20.10.2020 um 04:53
    • #1200224
    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 Beiträge

    Italien

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

    Can you provide some working example, please

    Tanks
  • @thomas
    Administrator

    348 Beiträge

    Belgien

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

    Can you provide some working example, please

    Tanks
    • Erstellt am 28.10.2020 um 04:36
    • #1203146
    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Link (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 Beiträge

    Italien

    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Link (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
    • Erstellt am 28.10.2020 um 05:54
    • #1203203
    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 Beiträge

    Italien

    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 :

    Link (https)



    Thanks and Best regards.

Werden Sie Mitglied der Sammler-Community!

Anmelden Einloggen