question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to send empty object

See original GitHub issue

I am trying to send empty object but retrofit clears out the param with empty or null values. How to make retrofit send empty object or array.

Use case: I am sending additional_fields array param as: additional_fields[0]=2, additional_fields[1]=3 using @Field etc. which is working perfectly. But when I am sending additional_fields=[] with an empty array, retrofit clears out this field and doesn’t send it. I am doing it in PATCH call as the user can update its preferences for additional_fields which can have some value or no value.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vipulasricommented, Apr 25, 2018

@JakeWharton Please help me out with this.

0reactions
operandocommented, Oct 18, 2018

@vipulasri I had the same problem.

You can send empty object or array by using FormBody.Builder. I wrote a sample code. please refer. https://gist.github.com/operando/9e7ebeece0e3c8a637580ee100d77834

// Create RequestBody
FormBody.Builder builder = new FormBody.Builder();
List<String> additionalFields = ...;
if (additionalFields.isEmpty()) {
    builder.add("additional_fields[]", "");
} else {
    for (String additionalField : additionalFields) {
        builder.add("additional_fields[]", additionalField);
    }
}

// Send
Api.editApi(builder.build()).enqueue(callback);

// API
interface Api {
    @PUT("put_api")
    Call<Void> editApi(@Body RequestBody body);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass empty value to the Object[] variable in java [closed]
To declare institutes empty: public Object[] institutes = new Object[0];. Note that this declares institutes as an empty array, ...
Read more >
Handling JSON null and empty arrays and objects - IBM
Handling null and empty arrays and objects used in JSON data is described. JSON data has the concept of null and empty arrays...
Read more >
How to Check if Object is Empty in JavaScript - Samantha Ming
We can use the built-in Object.keys method to check for an empty object. const empty = {}; Object ...
Read more >
provide empty object/array instance #2925 - GitHub
... to return an 'empty object'. at present I had to create a new object/array example: private JsonObject getJsonObjectByName(String name) { if (condition) ......
Read more >
42961 (REST API: Cannot pass empty object url encoded data)
When posting using json, empty array is handled correctly. When using querystring, it fails. Please see ​https://github.com/woocommerce/woocommerce/issues/18249.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found