将JSON POST发送到Django App

时间:2012-08-04 20:36:16

标签: java django json rest

我有一些Java代码尝试将POST数据发送到t Django应用程序。但是,视图从未被调用过。如果我将相同的URL粘贴到我的浏览器中,则会调用Django视图。我不知道我错过了什么,但Java编写必定是错误的。

这是执行write的Java函数:

public void executeWrite(String requestUrl, JsonObject jsonObject)
{
    DataInputStream  input = null;
    try
    {
        URL                 url;
        HttpURLConnection urlConn;
        DataOutputStream printout;

        System.out.println(requestUrl);
        // URL of CGI-Bin script.
        url = new URL (requestUrl);
        // URL connection channel.
        urlConn = (HttpURLConnection)url.openConnection();
        // Let the run-time system (RTS) know that we want input.
        urlConn.setDoInput (true);
        // Let the RTS know that we want to do output.
        urlConn.setDoOutput (true);
        // No caching, we want the real thing.
        urlConn.setUseCaches (false);
        // Specify the content type.
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("content-type","application/json; charset=utf-8");

        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(jsonObject.toString());
        wr.flush();
        wr.close();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

现在传递给函数的requestURL直接对应于Django视图的那个。 requestURL是:

http://127.0.0.1:8000/events/rest/33456/create

这是Django Urlconfig:

(r'^events/rest/(?P<key>\d+)/create', 'events.views.restCreateEvent'),

最后,这是Java代码永远不会调用的视图

@csrf_exempt 
def restCreateEvent(request, key):
    #doesn't really matter what is in here it never runs

那么,我错误的是Django服务器从未收到POST请求?我花了大约2个小时试图解决它,我发现Java代码没有任何问题。显然有些事情是错误的。

3 个答案:

答案 0 :(得分:1)

确保您的视图为csrf exempt,因为您没有从Java请求中发送相应的CSRF令牌。

答案 1 :(得分:0)

我认为crsf事实上是问题所在。一旦我添加了我稍微更改了Java代码并且它有效。我仍然不确定微软的Java错误是什么,这是工作的Java代码。

public void executeWrite(String requestUrl, JsonObject jsonObject)
{
    InputStreamReader  input = null;
    try
    {
        URL                 url;
        HttpURLConnection urlConn;
        DataOutputStream printout;

        System.out.println(requestUrl);
        // URL of CGI-Bin script.
        url = new URL (requestUrl);
        // URL connection channel.
        urlConn = (HttpURLConnection)url.openConnection();
        // Let the run-time system (RTS) know that we want input.
        urlConn.setDoInput (true);
        // Let the RTS know that we want to do output.
        urlConn.setDoOutput (true);
        // No caching, we want the real thing.
        urlConn.setUseCaches (false);
        // Specify the content type.
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("content-type","application/json; charset=utf-8");

        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(jsonObject.toString());
        wr.flush();
        wr.close();

        input = new InputStreamReader (urlConn.getInputStream ());
        String response = UserInterface.read(new BufferedReader(input));

        if(response.length() > 0)
        {
            System.out.println("Response:" + response);
        }

        input.close();
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }
}

答案 2 :(得分:-1)

我记得使用“ POST”类型时,URL需要更改为“ http://127.0.0.1:8000/events/rest/33456/create/”。