How do I send a POST request as a JSON?

If your server is expecting the POST request to be json, then you would need to add a header, and also serialize the data for your request… Python 2.x import json import urllib2 data = { ‘ids’: [12, 3, 4, 5, 6] } req = urllib2.Request(‘http://example.com/api/posts/create’) req.add_header(‘Content-Type’, ‘application/json’) response = urllib2.urlopen(req, json.dumps(data)) Python 3.x https://stackoverflow.com/a/26876308/496445 …

Read more

nginx upload client_max_body_size issue

nginx “fails fast” when the client informs it that it’s going to send a body larger than the client_max_body_size by sending a 413 response and closing the connection. Most clients don’t read responses until the entire request body is sent. Because nginx closes the connection, the client sends data to the closed socket, causing a …

Read more

How do you Programmatically Download a Webpage in Java

I’d use a decent HTML parser like Jsoup. It’s then as easy as: String html = Jsoup.connect(“http://stackoverflow.com”).get().html(); It handles GZIP and chunked responses and character encoding fully transparently. It offers more advantages as well, like HTML traversing and manipulation by CSS selectors like as jQuery can do. You only have to grab it as Document, …

Read more

How do I make an http request using cookies on Android?

It turns out that Google Android ships with Apache HttpClient 4.0, and I was able to figure out how to do it using the “Form based logon” example in the HttpClient docs: https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientFormLogin.java import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; …

Read more

What exactly is an HTTP Entity?

An HTTP entity is the majority of an HTTP request or response, consisting of some of the headers and the body, if present. It seems to be the entire request or response without the request or status line (although only certain header fields are considered part of the entity). To illustrate; here’s a request: POST …

Read more