How to send post data via JSON using Java

  • cartamoneta-com

    36 berichten

    Italië

    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
    Beheerder

    348 berichten

    België

    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
    • Aangemaakt 12 okt 2020 op 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 berichten

    Italië

    It works!

    Thanks
  • cartamoneta-com

    36 berichten

    Italië

    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
    Beheerder

    348 berichten

    België

    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.
    • Aangemaakt 19 okt 2020 op 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 berichten

    Italië

    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
    Beheerder

    348 berichten

    België

    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::";
    • Aangemaakt 19 okt 2020 op 06:16
    • #1199941
    Can you give me the id or link of the concerned sale?
  • cartamoneta-com

    36 berichten

    Italië

    Can you give me the id or link of the concerned sale?
    • Aangemaakt 19 okt 2020 op 07:45
    • #1199968
    This is the link of the test item (#1111365128) that i'm trying to update:
    Link (https)
  • @thomas
    Beheerder

    348 berichten

    België

    This is the link of the test item (#1111365128) that i'm trying to update:
    Link (https)
    • Aangemaakt 20 okt 2020 op 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 berichten

    Italië

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

    Can you provide some working example, please

    Tanks
  • @thomas
    Beheerder

    348 berichten

    België

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

    Can you provide some working example, please

    Tanks
    • Aangemaakt 28 okt 2020 op 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 berichten

    Italië

    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
    • Aangemaakt 28 okt 2020 op 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 berichten

    Italië

    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.

Word lid van de verzamelaars-community!

Registreren Aanmelden