How to send post data via JSON using Java

  • cartamoneta-com

    36 messages

    Italie

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

    String url = " Lien (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
    Administrateur

    348 messages

    Belgique

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

    String url = " Lien (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
    • Posté le 12 oct. 2020 à 09:59
    • #1198066
    Hello cartamoneta-com,

    Using a library can help you simplify the process.

    For example, with uniRest: Lien (http)

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.post(" Lien (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 messages

    Italie

    It works!

    Thanks
  • cartamoneta-com

    36 messages

    Italie

    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 = " Lien (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
    Administrateur

    348 messages

    Belgique

    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 = " Lien (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.
    • Posté le 19 oct. 2020 à 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 messages

    Italie

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

    My actual url is this:

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

    348 messages

    Belgique

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

    My actual url is this:

    String apiUpdate = " Lien (https) " + "/item/" + itemID + "?token=::TOKEN::";
    • Posté le 19 oct. 2020 à 06:16
    • #1199941
    Can you give me the id or link of the concerned sale?
  • cartamoneta-com

    36 messages

    Italie

    Can you give me the id or link of the concerned sale?
    • Posté le 19 oct. 2020 à 07:45
    • #1199968
    This is the link of the test item (#1111365128) that i'm trying to update:
    Lien (https)
  • @thomas
    Administrateur

    348 messages

    Belgique

    This is the link of the test item (#1111365128) that i'm trying to update:
    Lien (https)
    • Posté le 20 oct. 2020 à 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 messages

    Italie

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

    Can you provide some working example, please

    Tanks
  • @thomas
    Administrateur

    348 messages

    Belgique

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

    Can you provide some working example, please

    Tanks
    • Posté le 28 oct. 2020 à 04:36
    • #1203146
    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Lien (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 messages

    Italie

    Hello cartamoneta-com,

    Unirest.setTimeouts(0, 0);
    HttpResponse response = Unirest.put(" Lien (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
    • Posté le 28 oct. 2020 à 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 messages

    Italie

    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 :

    Lien (https)



    Thanks and Best regards.

Rejoignez la communauté des collectionneurs !

S'inscrire Se connecter