Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:42:22+00:00 2026-06-10T16:42:22+00:00

I have an android app posting to my sinatra service. Earlier I was not

  • 0

I have an android app posting to my sinatra service. Earlier I was not able to read parameters on my sinatra service. However, after I set content type to “x-www-form-urlencoded”. I was able to see params but not quite how I wanted. I am getting something this as my params of the request at my sinatra service.

{"{\"user\":{\"gender\":\"female\"},\"session_id\":\"7a13fd20-9ad9-45c2-b308-
8f390b4747f8\"}"=> nil, "splat"=>["update_profile.json"], "captures"=>["update_profile.json"]}

This is how I am making request from my app.

StringEntity se;                    
se = new StringEntity(getJsonObjectfromNameValueList(params.get_params(), "user");
se.setContentType("application/x-www-form-urlencoded");
postRequest.setEntity(se);



private JSONObject getJsonObjectfromNameValueList(ArrayList<NameValuePair> _params, String RootName) {
    JSONObject rootjsonObject = new JSONObject();
    JSONObject jsonObject = new JSONObject();

    if (_params != null) {
        if (!_params.isEmpty()) {
            for (NameValuePair p : _params) {
                try {
                    if (p.getName().equals(ApplicationFacade.SESSION_ID))
                        rootjsonObject.put((String) p.getName(), (String) p.getValue());
                    else
                        jsonObject.put((String) p.getName(), (String) p.getValue());
                } catch (JSONException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }

    try {
        rootjsonObject.put(RootName, jsonObject);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return rootjsonObject;
}

I tried using the method given by:How to make a nested Json object in Java?

This is how I made my request using the method mentioned in the link.

public ArrayList<NameValuePair> getJsonObjectfromNameValueList(ArrayList<NameValuePair> _params, String RootName){
    ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
    JSONObject jsonObject = new JSONObject();
    if (_params != null) {
        if (!_params.isEmpty()) {
            for (NameValuePair p : _params) {
                try {
                    if (p.getName().equals(ApplicationFacade.SESSION_ID))
                        arrayList.add(new BasicNameValuePair((String) p.getName(), (String) p.getValue()));
                    else
                        jsonObject.put((String) p.getName(), (String) p.getValue());
                } catch (JSONException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }
    arrayList.add(new BasicNameValuePair(RootName, jsonObject.toString()));
    return arrayList;
}

The response I got after making the above change was this:

{"session_id"=>"958c7dee-f12c-49ec-ab0c-932e9a4ed173",
"user"=>"[gender=male]",
 "splat"=>["update_profile.json"],
 "captures"=>["update_profile.json"]}

Pretty close but that “gender=male” is not desirable. I need to pass these arguments to another service blindly, so I need to get them right.

The params that I want at my sinatra service are following.

{"session_id" : "958c7dee-f12c-49ec-ab0c-932e9a4ed173",
 "user": 
 {
   "gender" : "male"
 }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:42:23+00:00Added an answer on June 10, 2026 at 4:42 pm

    It turns out that the problem is with the way Sinatra gets its params.
    I was able to solve my problem using Kyle’s awesome response to this post: Sinatra controller params method coming in empty on JSON post request

    Code:

    before do
      if request.request_method == "POST" and request.content_type=="application/json"
        body_parameters = request.body.read
        parsed = body_parameters && body_parameters.length >= 2 ? JSON.parse(body_parameters) : nil
        params.merge!(parsed)
      end
    end
    

    Alternate Method:
    However, I found a better solution to deal with it, by adding a middleware to do the same for me. I used rack-contrib gem.
    Following are the changes I did in my code:

    EDIT:

    use git to get the version where issue related to content type application/json;charset=UTF-8 is fixed

    Gemfile:

    gem 'rack-contrib', git: 'git@github.com:rack/rack-contrib', ref: 'b7237381e412852435d87100a37add67b2cfbb63'
    

    config.ru:

    use Rack::PostBodyContentTypeParser
    

    source: http://jaywiggins.com/2010/03/using-rack-middleware-to-parse-json/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android app modeled after the LunarLander example by Google. I am
I have android app . i should pass the data from android to web
I have Android client app that communicates with server using Socket . On my
I have an Android app, that is using the SIM Toolkit. The application has
I have an android app where in my Main activity I can play music
I have an android app that I am trying to launch and it gives
I have an android app which has native code. The native code needs to
I have an android app which let the user to submit a form ,
I have an android app where the user paints, moves and reshapes some objects
I have an Android app that has an Application class and I have another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.