0

How would you send a request (from python) that ultimately ends up looking like this in the live header on the web page (http://localhost:8080):

POST /rest/marker/
json=%7B%22label%22%3A%22w%22%2C%22folderId%22%3Anull%2C%22url%22%3A%22%23FF0000%22%2C%22comments%22%3A%22%22%2C%22position%22%3A%7B%22lat%22%3A39.426912683948%2C%22lng%22%3A-120.20892536635%7D%7D&tid=0V7V

In better syntax, the POST request looks like this:

URL=http://localhost:8080/rest/marker/
json: {"label":"my label here","folderId":null, "url":"#FF0000","comments":"","position":{"lat":39.2965796259061,"lng":-120.16708374023438}}
tid: "0V7V"

(please ignore the data values, they are different in each of these tests)

I've tried several variations like the following:

a=requests.post("http://localhost:8080/rest/marker",data="json=%7B%22label%22%3A%22stuff%22%2C%22folderId%22%3Anull%2C%22url%22%3A%22%2300FF00%22%2C%22comments%22%3A%22%22%2C%22position%22%3A%7B%22lat%22%3A39.418%2C%22lng%22%3A-120.2%7D%7D&tid=0V7V")

a=requests.post("http://localhost:8080/rest/marker",json={"label":"stuff","folderId":"null","url":"#FF0000","comments":"","position":{"lat":39.4112,"lng":-120.2},"tid":"0V7V"})

a=requests.post("http://localhost:8080/rest/marker/",json={"label":"stuff","folderId":"null","url":"#FF0000","comments":"","position":{"lat":39.4112,"lng":-120.2}},data={"tid":"0V7V"})

The stacktrace I get in the response text always starts with this, which probably just indicates I'm doing it wrong:

java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to
net.sf.json.JSONObject

What's the right way to do this?

Anentropic
  • 32,188
  • 12
  • 99
  • 147
Tom Grundy
  • 736
  • 5
  • 26

1 Answers1

0

Try this:

import json

payload = {
    "label": "my label here",
    "folderId": None,  # converted to null by json serializer
    "url": "#FF0000",
    "comments":"",
    "position": {
        "lat":39.2965796259061,
        "lng":-120.16708374023438,
    }
}

response = requests.post(
    "http://localhost:8080/rest/marker",
    data={'json': json.dumps(payload), 'tid': '0V7V'}
)

From details in your question it sounds like the server expects you to post a form containing a field called json with a json-serialized string as its value.

When you send form data the values need to be urlencoded ...however requests will automatically do this for you. You just need to pass in a dict of your form data, though you will still have to json serialize the payload as the value of json key.

The problem with your first example is you have already urlencoded your payload, so when you pass it to requests it will end up double-encoded.

In your second example you are telling requests to send a json-serialized payload, just as a raw POST body rather than as form data.

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • that did get a different message in the response text, now it starts with

    Status Code: 500

    Exception:
    Stacktrace:
    java.lang.NullPointerException
    – Tom Grundy Sep 24 '15 at 16:05
  • is there a way to see the built POST string that is actually getting sent, in the same syntax as the live header? – Tom Grundy Sep 24 '15 at 16:05
  • maybe you need to debug your java server – Anentropic Sep 24 '15 at 16:07
  • maybe. I'm not the author of the java servlet. The live header was captured while using an actual GUI feature of the java application that creates the marker, and, it is known and illustrated to work - so, all I really need to do is reproduce that exact POST request. – Tom Grundy Sep 24 '15 at 16:11
  • you could try a proxy eg http://www.charlesproxy.com/ to inspect the contents of requests being sent... I'm sure you could also step through the `post()` call eg with [ipdb](https://pypi.python.org/pypi/ipdb) to see what request `requests` prepares before it's sent – Anentropic Sep 24 '15 at 16:11
  • for viewing the prepared request content, I'll try the method in the accepted answer in this question: http://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw – Tom Grundy Sep 25 '15 at 13:27
  • yeah it's a very good find. So, I can reproduce the request body exactly, and I added the same Content-Type header, but still no go. At this point I'll take it up with the developer since I can show that I'm producing the same request. I'll update the status here after hearing back. – Tom Grundy Sep 25 '15 at 14:14