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

  • Home
  • SEARCH
  • 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 9220797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:25:57+00:00 2026-06-18T03:25:57+00:00

In my app I’m going to be sending JSON in a POST request to

  • 0

In my app I’m going to be sending JSON in a POST request to the server.

I want to test to make sure I’m sending the right values.

Since I’m using Robolectric, my conclusion is that I should get the request that is being sent to the FakeHttpLayer, pull out the JSON, and test that it matches my expectations.

Sounds simple, but I’m having a hell of a time figuring out how to see what JSON I’ve POSTed.

My code vaguely looks like this:

HttpClient client = new DefaultHttpClient();
HttpResponse response;
JSONObject json = new JSONObject();
try{
    HttpPost post = new HttpPost("http://google.com");
    json.put("blah", "blah");
    StringEntity se = new StringEntity( "JSON: " + json.toString());
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    post.setEntity(se);
    response = client.execute(post);
} catch(Exception e){
  e.printStackTrace();
}

I want to have something which is like

assertTrue(myRequestJSON.matches("blah"));

But I can’t seem to get that using anything inside of

Robolectric.getFakeHttpLayer().getLastSentHttpRequestInfo();

…or any of the other variations on that.

Help?

By the way, I’m certainly open to thinking about this in a different way, if you think my testing approach is misguided.

Thank you!

EDIT: I realized I had “myResponseJSON” in my dummy code rather than “myRequestJSON,” which may have made it unclear–fixed.

  • 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-18T03:25:58+00:00Added an answer on June 18, 2026 at 3:25 am

    I was completely wrong in my previous solution. You can do this rather simply it would appear. I found the solution (predictably) in the Robolectric example code:

    HttpPost sentHttpRequest = (HttpPost) Robolectric.getSentHttpRequest(0);
    StringEntity entity = (StringEntity) sentHttpRequest.getEntity();
    String sentPostBody = fromStream(entity.getContent());
    assertThat(sentPostBody, equalTo("a post body"));
    assertThat(entity.getContentType().getValue(), equalTo("text/plain; charset=UTF-8"));
    

    (from here)

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

Sidebar

Related Questions

@app.route(/<requested_username>/<requested_team>, methods=['GET', 'POST']) ^How do I make this so that no matter what is
App:MFC backed by oracle Unit testing framework:googletest I am going to unit testing the
app.yaml application: cloudymovie version: 1 runtime: java welcome_files: - test.jsp handlers: - url: /test/*
app.get('/',function(req,res){ res.render('home'); // I want the template to be able to access the flash
app.post('/register', express.BodyParser(), function (req, res){ var newu = new UserModel({}); newu.user = req.body.nuser; newu.pass
The app I am writing deals with utility service addresses, and right now I
My app needs to be able to disconnect from a server and connect to
App reads JSON data. Then it would put it to whe listview (correctly) but
app.post('/login', function (req, res) { var login = req.body.login; var pass = req.body.pass; var
app link: http://itunes.apple.com/en/app/quick-tweet/id505987287?mt=12 How can i make something like that circle? is that a

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.