HTTP POST request problem

edited January 2016 in Library Questions

Hi everyone! I have a problem with HTTP request library. I use this code:

import http.requests.*;

import java.net.*;
import java.io.*;
import java.util.*;

String API="myApi"; // Api code
String answer="My answer"; // answer text
String messageTo="1111111"; // example integer
String urlAnswer = URLEncoder.encode(trim(answer)); // encode answer string
PostRequest post = new PostRequest("https://"+"api.telegram.org/bot"+API+"/sendmessage?chat_id="+messageTo+"&text="+urlAnswer);
post.send();

and status monitor show me this error message:

java.lang.NullPointerException
    at http.requests.PostRequest.send(Unknown Source)
    at sketch_160109a.setup(sketch_160109a.java:33)
    at processing.core.PApplet.handleDraw(PApplet.java:2373)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1523)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

But GET request work perfect.

Can somebody explain me how I fix this problem?

I use Processing 3.0 on MacOS 10.9.5

Answers

  • Some trouble with code insert. Correct line: PostRequest post = new PostRequest("https://api.telegram.org/bot"+API+"/sendmessage?chat_id="+messageTo+"&text="+urlAnswer);

  • edited January 2016

    Some trouble with code insert.

    Separate the http:// part from the rest of the URL when posting here to avoid the glitch: >-)

    PostRequest post = new PostRequest(
      "https://" + "api.Telegram.org/bot" +
      API + "/sendmessage?chat_id=" +
      messageTo + "&text=" + urlAnswer);
    
  • // forum.Processing.org/two/discussion/14354/http-post-request-problem
    
    import http.requests.PostRequest;
    import java.net.URLEncoder;
    
    String API = "myApi"; // Api code
    String answer = "My answer"; // answer text
    String messageTo = "1111111"; // example integer
    String urlAnswer = URLEncoder.encode(answer); // encode answer string
    
    String URL = "https://" + "api.Telegram.org/bot"
      + API + "/sendmessage?chat_id=" + messageTo + "&text=" + urlAnswer;
    
    PostRequest post = new PostRequest(URL);
    
    println(URL);
    println(post, urlAnswer);
    
    post.send();
    
  • edited January 2016

    Separate the http:// part from the rest of the URL when posting here to avoid the glitch:

    It's my firs post! Thank's a lot! It's looks good now. But it's can't fix a real problem. ;)

  • edited January 2016

    Indeed. Perhaps the resultant URL passed to PostRequest's constructor isn't valid? :-/

  • Indeed. Perhaps the resultant URL passed to PostRequest's constructor isn't valid?

    If I past this url with correct API key and correct chat_id with some text in the end of this construction in web browser I have correct answer. The problem on Processing side.

  • edited January 2016

    I use code from HTTP request library example:

    import http.requests.*;
    
    public void setup() 
    {
        size(400,400);
        smooth();
    
        PostRequest post = new PostRequest("http://"+"httprocessing.heroku.com");
        post.addData("name", "Rune");
        post.send();
        System.out.println("Reponse Content: " + post.getContent());
        System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
    }
    

    and I have the same problem.

  • Indeed. Perhaps the resultant URL passed to PostRequest's constructor isn't valid?

    Hey Yeah! I install old library 0.1.2 and my problem is gone!

  • an issue has been raised on the github repo for this bug (last october) but it hasn't been fixed yet

    https://github.com/runemadsen/HTTP-Requests-for-Processing/issues/16

  • edited January 2016

    an issue has been raised on the github repo for this bug (last october) but it hasn't been fixed yet

    I don't know why, but new version (0.1.3) of HTTP request don't work correctly with POST method. I try add header fixes but nothing happened. At finally I install 0.1.2 version and problem is gone!

  • yes, 0.1.2 is fine. you lose BasicAuth capability (which you probably aren't using) but you don't get the null pointer exception.

  • edited January 2016

    Had the same problem. I can't seem to get 0.1.2 to work either though.

    Different error. Can anyone help? Edit: This is for an Android app, incidentally. Maybe it is not intended for this?

    Here's my utterly stripped down code, for which it still happens (for both POST and GET requests). I've tried 0.1.2 and the version previous to that (0.1 I think?)

    import http.requests.*;
    public void setup() 
    {
        size(400,400); 
        smooth();
        GetRequest get = new GetRequest("https:// requestb.in/1lghsyb1");
        get.send();
        println("Reponse Content: " + get.getContent());
        println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
    }
    

    Here's the error I get with the emulator:

    FATAL EXCEPTION: Animation Thread
    java.lang.NoSuchMethodError: org.apache.http.util.EntityUtils.consume
        at http.requests.GetRequest.send(Unknown Source)
        at processing.test.get.get.setup(get.java:31)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:856)
    

    and on my device:

    FATAL EXCEPTION: Animation Thread
    Process: processing.test.get, PID: 2129
    java.lang.NoSuchMethodError: No static method consume(Lorg/apache/http/HttpEntity;)V in class Lorg/apache/http/util/EntityUtils; or its super classes (declaration of 'org.apache.http.util.EntityUtils' appears in /system/framework/ext.jar)
        at http.requests.GetRequest.send(Unknown Source)
        at processing.test.get.get.setup(get.java:31)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
    
  • I think I have the same problem sending POST and GET requests. I use latest version of HttpRequest (which is 0.1.4) and I use an Android device. After solving a lot of issues I ended with an error I cannot resolve. My code is

      PostRequest post = new PostRequest("http://gtricho.dideles.gr/index.php", "UTF-8");
       post.addData("Fall","Any");
       post.addHeader("Content-Type", "application/json");
       post.send();
       println("Reponse Content: " + post.getContent());
       println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
    

    and the error I get is:

    FATAL EXCEPTION: Thread-198
    java.lang.NoSuchMethodError: org.apache.http.util.EntityUtils.consume
        at http.requests.PostRequest.send(Unknown Source)
        at processing.test.sensors.sensors$1.run(sensors.java:80)
        at java.lang.Thread.run(Thread.java:856)
    

    Has anyone succeeded sending post requests to a web page?....

  • The problem could be that HTTP Requests library uses an older version of Apache's HTTP Client.

  • Thank you for your answer. The solution then should come from the creators of the library by rebuilding it? Or maybe I could install an older Apache server edition?

  • If I were you, I'd just learn to use HTTP Client library directly.

Sign In or Register to comment.