Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set zero-length content on an HTTP GET request #145

Closed
wonderfly opened this issue Jan 10, 2015 · 8 comments
Closed

Ability to set zero-length content on an HTTP GET request #145

wonderfly opened this issue Jan 10, 2015 · 8 comments
Assignees
Labels
imported priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@wonderfly
Copy link
Contributor

From zcmk...@gmail.com on February 23, 2011 19:13:15

Version of google-api-java-client (e.g. 1.2.1-alpha)? 1.3.1-alpha Java environment (e.g. Java 6, Android 2.2, App Engine 1.3.7)? Java 6 Describe the problem. As yan...@google.com concerned in Issue 68 ,"Please don't use "content-type" header directly and instead use an instance of HttpContent.If the HttpContent of a request is null, we will assume there is no content."

but how to make an instance httpcontent? For example I need make request as this:
Content-Length=0 Content-Type=text/plain.which httpcontent should I use GZip,urlencode or logcontent?

the other way is use NetHttpRequest,but I can't use NetHttpRequest.setcontent().

HttpRequest request = transport.buildGetRequest();is correct,but HttpRequest doesn't have method setcontent()

NetHttpRequest request = transport.buildGetRequest(); It said "NetHttpRequest cannot be resolved to a type".When I
import com.google.api.client.http.javanet.NetHttpRequest; It said "The type com.google.api.client.http.javanet.NetHttpRequest is not visible."
The declaration of NetHttpQequest is
final class NetHttpRequest extends LowLevelHttpRequest How would you expect it to be fixed? would mend give a sample of googlestorage?

here is my code:
public GoogleStorage(String accessKey,String secret) throws IOException
{
//work for fiddle
//System.setProperty("http.proxyHost", "localhost");
//System.setProperty("http.proxyPort", "8888");
//System.setProperty("https.proxyHost", "localhost");
//System.setProperty("https.proxyPort", "8888");

    transport = new NetHttpTransport();
     GoogleHeaders headers=new GoogleHeaders();
     transport.defaultHeaders=headers;
             //Is this Parser right ? I also want use xmlparser to parser 
             //response err message.
     transport.addParser(new JsonCParser());
     GoogleStorageAuthentication.authorize(transport, accessKey, secret);


}



public void list() throws IOException
{


    HttpRequest request = transport.buildGetRequest();

    request.setUrl("http://commondatastorage.googleapis.com");
    request.headers.put("Date",Now());

    //this is not work correct
    //request.headers.put("Content-Length","0");
    //request.headers.put("Content-Type","text/plain");

    try {
        HttpResponse response = request.execute();
        System.out.println(response.parseAsString());
    }catch (HttpResponseException e){
            //this is not work correct ,how to parse response err message?
        System.out.println(e.response.parseAsString()));
    };

Original issue: http://code.google.com/p/google-api-java-client/issues/detail?id=130

@wonderfly wonderfly added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. imported priority: p2 Moderately-important priority. Fix may not be included in next release. 1 star labels Jan 10, 2015
@wonderfly
Copy link
Contributor Author

From yan...@google.com on February 23, 2011 19:52:03

Try this:
InputStreamContent content = new InputStreamContent();
content.setByteArrayInput(new byte[0]);
content.type = "text/plain";
request.content = content;

Status: ByDesign

@wonderfly wonderfly self-assigned this Jan 10, 2015
@wonderfly
Copy link
Contributor Author

From zcmk...@gmail.com on February 24, 2011 05:43:13

Thank you for the answer.I tried the code,it seems still doesn't work correct.
my code like this:
public void list() throws IOException
{

    HttpRequest request = transport.buildGetRequest();
    request.setUrl("http://commondatastorage.googleapis.com");


    InputStreamContent content = new InputStreamContent();
        content.setByteArrayInput(new byte[0]);
        content.type = "text/plain";
        request.content = content;

        request.headers.put("Date",Now());

    try {
        HttpResponse response = request.execute();
        System.out.println(response.parseAsString());
    }catch (HttpResponseException e){
        System.out.println(e.response.parseAsString());
    };


}

I use fiddle catch to package, include your code the Httpmethod is post
del the code httpmethod is correct :get.
I trace the code in the httprequest.class ,the httpmethod still is "get"
is there a point think content has a inputstream then set httpmethod to post?
by the way if not set content googlestoageauthorize doesn't work correct.
another silly question:how to parser the googlestorage error status,
I found the code in issue #9 :
XmlHttpParser errorParser = new XmlHttpParser();
errorParser.contentType = "application/vnd.google.gdata.error+xml";
errorParser.namespaceDictionary = errorNamespaceDictionary;
transport.addParser(errorParser);
but errorNamespaceDictionary isn't valid.And doest is need to design a errorstatus class to parser it?

@wonderfly
Copy link
Contributor Author

From zcmk...@gmail.com on February 27, 2011 00:14:22

There seems has a bug.I traced in httprequest.java:
HttpResponse response = new HttpResponse(transport, lowLevelHttpRequest.execute());
before this lowLevelHttpRequest.connection.method is GET
after this lowLevelHttpRequest.connection.method is POST

@wonderfly
Copy link
Contributor Author

From zcmk...@gmail.com on March 14, 2011 04:38:47

In NetHttpRequest.java ,public LowLevelHttpResponse execute():
content.writeTo(connection.getOutputStream());
if you call connection.getOutputStream(),connection.method change from "GET" to "POST";
since all GET content-length=0, I suggest the code to this:
if (content != null) {
connection.setDoOutput(true);
String contentType = content.getType();
if (contentType != null) {
addHeader("Content-Type", contentType);
}
String contentEncoding = content.getEncoding();
if (contentEncoding != null) {
addHeader("Content-Encoding", contentEncoding);
}
long contentLength = content.getLength();
if (contentLength >= 0) {
addHeader("Content-Length", Long.toString(contentLength));
}
if (contentLength > 0) {
connection.setDoOutput(true);
content.writeTo(connection.getOutputStream());
}
}

@wonderfly
Copy link
Contributor Author

From yan...@google.com on March 22, 2011 07:26:02

Need to try to reproduce it.

Status: New

@wonderfly
Copy link
Contributor Author

From yan...@google.com on April 05, 2011 12:31:44

Taking a look at the JavaDoc for HttpURLConnection on Android: http://developer.android.com/reference/java/net/HttpURLConnection.html It says that calling setDoOutput(true) changes the method from GET to POST.

Summary: Ability to set zero-length content on a GET request
Status: Accepted
Cc: jasonhall@google.com
Labels: -Type-Defect Type-Enhancement Milestone-Version1.4.0 Component-HTTP

@wonderfly
Copy link
Contributor Author

From yan...@google.com on April 05, 2011 12:53:20

http://codereview.appspot.com/4354051/

Summary: Ability to set zero-length content on an HTTP GET request
Status: Started

@wonderfly
Copy link
Contributor Author

From yan...@google.com on April 05, 2011 12:56:38

Status: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

1 participant